Form1.cs
上传用户:bjelec
上传日期:2015-05-18
资源大小:30k
文件大小:4k
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.Threading;
- using System.Net.Sockets;
- namespace client
- {
- public partial class Form1 : Form
- {
- private IPAddress myIP = IPAddress.Parse("127.0.0.1");
- private IPEndPoint MyServer;
- private Socket sock;
- private bool flag = true;
-
- public Form1()
- {
- InitializeComponent();
- }
-
- private void targett()
- {
- Control.CheckForIllegalCrossThreadCalls = false;
- toolStripLabel1.Text = "与服务器端建立连接!";
- try
- {
- while (flag)
- {
- byte[] r_data = new byte[64];
- sock.Receive(r_data, r_data.Length, 0);
- string str = System.Text.Encoding.BigEndianUnicode.GetString(r_data);
- listBox1 .Items .Add (str.Substring(1, str.Length - 1) );
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void sendmsg(string sendtext)
- {
- try
- {
- byte[] s_data = new byte[64];
- s_data = System.Text.Encoding.BigEndianUnicode.GetBytes(sendtext.ToCharArray ());
- sock.Send(s_data, s_data.Length, 0);
- }
- catch
- {
- MessageBox.Show("连接尚未建立!无法发送!");
- }
- }
- private void Connect()
- {
- try
- {
- MyServer = new IPEndPoint(myIP, 6001);
- sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- sock.Connect(MyServer);
- Thread thread = new Thread(new ThreadStart(targett));
- thread.Start();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Connect();
-
- }
- private void button3_Click(object sender, EventArgs e)
- {
- listBox1.Items.Clear();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- sendmsg(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "我说:" + richTextBox1.Text);
- listBox1.Items.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "我说:" + richTextBox1.Text);
- richTextBox1.Clear();
- }
- //private void button4_Click(object sender, EventArgs e)
- //{
- // openFileDialog1.InitialDirectory ="c:\" ;
- // openFileDialog1.Filter = "全部文件|*.*|文本文件|*.txt";
- // if (openFileDialog1.ShowDialog() == DialogResult.OK)
- // {
- // textBox1.Text = openFileDialog1.FileName;
- // FileStream fs = new FileStream(textBox1.Text.ToString (), FileMode.OpenOrCreate);
- // textBox2.Text = fs.Length.ToString ()+"K";
- // }
- //}
- //private void button5_Click(object sender, EventArgs e)
- //{
- // Socket sensock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- // IPEndPoint ipep = new IPEndPoint(myIP, 6001);
- // try
- // {
- // if (textBox1.Text == "")
- // {
- // MessageBox.Show("文件名不能为空");
- // }
- // else
- // {
- // FileStream fs = new FileStream(textBox1.Text.ToString(), FileMode.OpenOrCreate);
- // BinaryReader r = new BinaryReader(fs);
- // byte[] postArray = r.ReadBytes((int)fs.Length);
- // sensock.Send(postArray);
- // }
- // }
- // catch (SocketException se)
- // {
- // MessageBox.Show(se.Message);
- // }
- //}
- }
- }