整理一份Socket代码,整理前辈的代码

http://www.cnblogs.com/yellowapplemylove/archive/2011/04/19/2021586.html

直接贴代码

一、客户端

 1 /// <summary>
 2     /// Client客户端
 3     /// </summary>
 4     public partial class ClientForm : Form
 5     {
 6         private IPAddress serverIp ;
 7         private IPEndPoint serverFullAddr;
 8         private Socket sock;
 9         public ClientForm()
10         {
11             InitializeComponent();
12             this.Load += (e, v) =>
13             {
14                 txtIP.Text = "127.0.0.1";
15                 txtPort.Text = "2014";
16             };
17         }
18         private void btnConnect_Click(object sender, EventArgs e)
19         {
20             try
21             {
22                 serverIp = IPAddress.Parse(txtIP.Text.Trim());
23                 serverFullAddr = new IPEndPoint(serverIp, int.Parse(txtPort.Text.Trim()));
24                 sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
25                 sock.Connect(serverFullAddr);//建立与远程主机的连接
26                 //启动新线程用于接收数据
27                 Thread t = new Thread(new ThreadStart(ReceiveMsg));
28                 t.Name = "接收消息";
29                 //一个线程或者是后台线程或者是前台线程。后台线程与前台线程类似,区别是后台线程不会防止进程终止。
30                 //一旦属于某一进程的所有前台线程都终止,公共语言运行库就会通过对任何仍然处于活动状态的后台线程调用 abort 来结束该进程。
31                 t.IsBackground = true;
32                 t.Start();
33             }
34             catch(Exception ex)
35             {
36                 MessageBox.Show(ex.Message);
37             }
38         }
39         private void ReceiveMsg()
40         {
41             try
42             {
43                 while (true)
44                 {
45                     byte[] byterec = new byte[100];
46                     this.sock.Receive(byterec);
47                     string strrec = System.Text.Encoding.UTF8.GetString(byterec);
48                     if (this.txtReceive.InvokeRequired)
49                     {
50                         this.txtReceive.Invoke(new EventHandler(ChangerText), new object[] { strrec, EventArgs.Empty });
51                     }
52                 }
53             }
54             catch (Exception ex)
55             {
56                 MessageBox.Show("接收服务器消息发生错误" + ex.Message);
57             }
58         }
59         private void ChangerText(object obj, EventArgs e)
60         {
61             string s = Convert.ToString(obj);
62             this.txtReceive.AppendText(s + Environment.NewLine);
63         }
64         private void btnSend_Click(object sender, EventArgs e)
65         {
66             byte[] byteSend =System.Text.Encoding.UTF8.GetBytes(this.txtSend.Text.ToCharArray());
67             try
68             {
69                 sock.Send(byteSend);
70             }
71             catch
72             {
73                 MessageBox.Show("向服务器发送数据错误");
74             }
75         }
76         private void btnClose_Click(object sender, EventArgs e)
77         {
78             try
79             {
80                 this.sock.Shutdown(SocketShutdown.Receive);
81                 this.sock.Close();
82                 Application.Exit();
83             }
84             catch
85             {
86                 MessageBox.Show("关闭连接发生错误");
87             }
88         }
89     }
View Code

相关文章:

  • 2022-12-23
  • 2021-06-16
  • 2022-02-23
  • 2021-09-20
  • 2021-09-29
  • 2021-12-01
  • 2021-06-30
  • 2021-10-11
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
  • 2021-08-28
  • 2021-11-26
  • 2021-06-05
相关资源
相似解决方案