示例目的:使用控制台项目模板分别新建一个服务器和一个客户端,实现两两通讯
1. 新建服务器项目
1 static TcpClient tcpClient; 2 static NetworkStream stream; 3 static void Main(string[] args) 4 { 5 try 6 { 7 var serverIPEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 13000); // 当前服务器使用的ip和端口 8 TcpListener tcpListener = new TcpListener(serverIPEndPoint); 9 tcpListener.Start(); 10 Console.WriteLine("服务端已启用......"); // 阻塞线程的执行,直到一个客户端连接 11 tcpClient = tcpListener.AcceptTcpClient(); 12 Console.WriteLine("已连接."); 13 stream = tcpClient.GetStream(); // 创建用于发送和接受数据的NetworkStream 14 15 #region 开启线程保持通讯 16 var t1 = new Thread(ReceiveMsg); 17 t1.Start(); 18 var t2 = new Thread(SendMsg); 19 t2.Start(); 20 #endregion 21 } 22 catch (Exception ex) 23 { 24 Console.WriteLine(ex.Message); 25 Console.ReadKey(); 26 } 27 28 } 29 30 /// <summary> 31 /// 发送消息 32 /// </summary> 33 static void SendMsg() 34 { 35 string message = string.Empty; 36 byte[] messageBytes; 37 try 38 { 39 while (true) 40 { 41 message = Console.ReadLine().ToString(); // 获取控制台字符串 42 messageBytes = Encoding.UTF8.GetBytes(message); // 将消息编码成字符串数组 43 stream.Write(messageBytes, 0, messageBytes.Length); 44 } 45 } 46 catch(Exception ex) 47 { 48 Console.WriteLine(ex.Message); 49 Console.ReadKey(); 50 } 51 52 } 53 54 /// <summary> 55 /// 接收消息 56 /// </summary> 57 static void ReceiveMsg() 58 { 59 byte[] buffer = new byte[1024]; // 预设最大接受1024个字节长度,可修改 60 int count = 0; 61 try 62 { 63 while ((count = stream.Read(buffer, 0, buffer.Length)) != 0) 64 { 65 Console.WriteLine($"{tcpClient.Client.LocalEndPoint.ToString()}:{Encoding.UTF8.GetString(buffer, 0, count)}"); 66 } 67 } 68 catch(Exception ex) 69 { 70 Console.WriteLine(ex.Message); 71 Console.ReadKey(); 72 } 73 74 }
2. 新建客户端项目
1 static TcpClient tcpClient; 2 static NetworkStream stream; 3 static void Main(string[] args) 4 { 5 try 6 { 7 var clientIPEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4196); // 当前客户端使用的ip和端口 8 tcpClient = new TcpClient(clientIPEndPoint); 9 var serverIPEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 13000); // 当前服务器使用的ip和端口 10 tcpClient.Connect(serverIPEndPoint); // 连接服务器 11 var isConnected = tcpClient.Connected; 12 Console.WriteLine("客户端已启用......"); 13 stream = tcpClient.GetStream(); // 创建用于发送和接受数据的NetworkStream 14 15 #region 开启线程保持通讯 16 var t1 = new Thread(ReceiveMsg); 17 t1.Start(); 18 var t2 = new Thread(SendMsg); 19 t2.Start(); 20 #endregion 21 } 22 catch (Exception ex) 23 { 24 Console.WriteLine(ex.Message); 25 Console.ReadKey(); 26 } 27 } 28 29 /// <summary> 30 /// 接收消息 31 /// </summary> 32 static void ReceiveMsg() 33 { 34 byte[] buffer = new byte[1024]; // 预设最大接受1024个字节长度,可修改 35 int count = 0; 36 try 37 { 38 while ((count = stream.Read(buffer, 0, buffer.Length)) != 0) 39 { 40 Console.WriteLine($"{tcpClient.Client.LocalEndPoint.ToString()}:{Encoding.UTF8.GetString(buffer, 0, count)}"); 41 } 42 } 43 catch (Exception ex) 44 { 45 Console.WriteLine(ex.Message); 46 Console.ReadKey(); 47 } 48 49 } 50 51 /// <summary> 52 /// 发送消息 53 /// </summary> 54 static void SendMsg() 55 { 56 string message = string.Empty; 57 byte[] messageBytes; 58 try 59 { 60 while (true) 61 { 62 message = Console.ReadLine().ToString(); // 获取控制台字符串 63 messageBytes = Encoding.UTF8.GetBytes(message); // 将消息编码成字符串数组 64 stream.Write(messageBytes, 0, messageBytes.Length); 65 } 66 } 67 catch (Exception ex) 68 { 69 Console.WriteLine(ex.Message); 70 Console.ReadKey(); 71 } 72 73 }
3.先启动服务器,再启动客户端,查看效果