iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了C# Socket系列2 TcpListener & TcpClient大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

这两个类是对socket的进一步封装

服务端

分享图片
 1  class Program
 2     {
 3         static void Main(String[] args)
 4         {
 5             TcpListener listener = null;
 6             listener = new TcpListener(Ipaddress.Parse("127.0.0.1"),9999);
 7             listener.Start();
 8             listener.beginAcceptTcpClient(new AsyncCallBACk(acceptCall),listener);
 9             Console.ReadKey();
10         }
11 
12         private static void acceptCall(IAsyncResult ar)
13         {
14             TcpListener lstn = ar.AsyncState as TcpListener;
15             TcpClient client = lstn.EndAcceptTcpClient(ar);
16             Task.Run(() => {
17                 String host = client.Client.RemoteEndPoint.ToString();
18                 NetworkStream stream = client.GetStream();
19                 try
20                 {
21                     while (true)
22                     {
23                         byte[] buffer = new byte[sizeof(int)];
24                         stream.Read(buffer,0,sizeof(int));
25                         int len = BitConverter.ToInt32(buffer,0);
26                         buffer = new byte[len];
27                         stream.Read(buffer,0,len);
28                         String resMsg = Encoding.UTF8.GetString(buffer);
29                         if (resMsg == "Q")
30                         {
31                             String message = "客户端" + host + "发出退出请求";
32                             Console.WriteLine(messagE);
33                             break;
34                         }
35                         else
36                         {
37                             String message = "收到" + host + "发的消息:" + resMsg;
38                             Console.WriteLine(messagE);
39                         }
40                     }
41                 }
42                 catch (Exception E)
43                 {
44 
45                     Console.WriteLine(e.messagE);
46                 }
47                 stream.Close();
48                 client.Close();
49             });
50           
51             lstn.beginAcceptTcpClient(new AsyncCallBACk(acceptCall),lstn);
52         }
53 
54        
55     }
View Code

客户端

class Program
    {
        static TcpClient client;
        static void Main(String[] args)
        {
            client = new TcpClient("127.0.0.1",9999);
            while (true)//发送
            {
                String msg = Console.ReadLine();
                if (msg == "Q")
                {
                    Sendmessage(msg);
                    break;
                }
                else
                    Sendmessage(msg);

            }
            
        }
        private static void Sendmessage(String msg)
        {
            if (client == null) return;
            if (client.Connected == falsE) return;
            byte[] data = Encoding.UTF8.GetBytes(msg);
            int len = data.Length;
            byte[] buffer = BitConverter.GetBytes(len);
            client.GetStream().Write(buffer,sizeof(int));
            client.GetStream().Write(data,data.Length);
        }
    }

大佬总结

以上是大佬教程为你收集整理的C# Socket系列2 TcpListener & TcpClient全部内容,希望文章能够帮你解决C# Socket系列2 TcpListener & TcpClient所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。