示例目的:使用控制台項目模板分別新建一個服務器和一個客戶端,實現兩兩通訊
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.先啟動服務器,再啟動客戶端,查看效果