C#socket通信-----多线程
Posted Snail_Go
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#socket通信-----多线程相关的知识,希望对你有一定的参考价值。
我在之前的socket通信的基础上做了一点改进,使用多线程来使用,程序更加简洁实用。不足之处请指教哦!
话不多说,之前的随笔也有介绍,直接上代码啦!
服务端socket(serverSocket):
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Net; 7 using System.Threading; 8 using System.Net.Sockets; 9 10 namespace serverSocketTest6 11 { 12 class Program 13 { 14 static Socket serverSocket; 15 static int port = 2723; 16 17 static void Main(string[] args) 18 { 19 Console.WriteLine("服务端:"); 20 21 //实例化socket类 22 serverSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); 23 IPAddress ip = IPAddress.Parse("173.168.5.15"); 24 //标识网络地址 25 EndPoint point = new IPEndPoint(ip,port); 26 //绑定IP地址和端口号 27 serverSocket.Bind(point); 28 29 //开始监听客户端的连接 30 serverSocket.Listen(10); 31 Console.WriteLine("等待客户端连接..."); 32 33 Thread myThread = new Thread(ListenClientSocket); 34 myThread.Start(); 35 36 37 Console.ReadKey(); 38 39 40 41 } 42 43 /// <summary> 44 /// 监听客户端 45 /// </summary> 46 static void ListenClientSocket() 47 { 48 while (true) 49 { 50 Socket clientSocket = serverSocket.Accept();//接受客户端的连接 51 Console.WriteLine("客户端连接成功"); 52 53 string message = "2017,新的开始,新年快乐!加油!继续努力!!!"; 54 byte[] data=Encoding.UTF8.GetBytes(message); 55 clientSocket.Send(data); 56 57 Thread receive = new Thread(receiveSocket);//receiveSocket 被传递的方法 58 receive.Start(clientSocket); //clientSocket 被传递的参数 59 } 60 61 } 62 63 /// <summary> 64 /// 接收来自客户端的消息 65 /// </summary> 66 static void receiveSocket(object clientSocket) ////// 这里的参数是线程中的参数,参数类型必须是object类型 67 { 68 Socket myClientSocket = (Socket)clientSocket; //// 将object类型的参数转换成socket类型 使用参数来启动线程,执行后面的代码 69 while (true) 70 { 71 byte[] data=new byte[1024]; 72 int length = myClientSocket.Receive(data); 73 string message=Encoding.UTF8.GetString(data,0,length); 74 Console.WriteLine("接收到来自客户端的消息:"+message); 75 76 77 } 78 } 79 } 80 }
客户端的代码基本没变~
客户端socket(clientSocket):
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.Threading; using System.Net.Sockets; namespace clientSocketTest6 { class Program { static Socket clientSocket; static int port = 2723; static void Main(string[] args) { Console.WriteLine("客户端:"); //实力化一个socket类 clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse("173.168.5.15"); //客户端请求与服务端的连接 clientSocket.Connect(ip, port); Console.WriteLine("服务器连接成功."); //接受数据 byte[] data = new byte[1024]; int length = clientSocket.Receive(data); string message = Encoding.UTF8.GetString(data, 0, length); Console.WriteLine("接受到来自服务端的祝福:" + message); //向服务端发送数居 for (int i = 0; i < 10; i++) { Thread.Sleep(2000);//使该线程暂停(休眠)2s string message2 = "Fighting!!"; //可以写成string message2=Console.ReadLine(); byte[] data2 = Encoding.UTF8.GetBytes(message2); clientSocket.Send(data2); Console.WriteLine("向服务端发送消息:{0}",message2); } Console.ReadKey(); } } }
以上是关于C#socket通信-----多线程的主要内容,如果未能解决你的问题,请参考以下文章