C# socket demo


1. 服務器端代碼

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Net;
 5 using System.Net.Sockets;
 6 using System.Text;
 7 using System.Threading;
 8 using System.Threading.Tasks;
 9 
10 namespace Sever
11 {
12     class Program
13     {
14         private static Socket severSocket = null;
15 
16         /// <summary>
17         /// 服務端程序
18         /// 1. 新建 socket,並綁定端口
19         /// 2. 接收客戶端連接
20         /// 3. 給客戶端發送信息
21         /// 4. 接收客戶端信息
22         /// 5. 斷開連接
23         /// </summary>
24         /// <param name="args"></param>
25         static void Main(string[] args)
26         {
27             severSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
28             IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 9999);
29             severSocket.Bind(endPoint);                 // 綁定
30             severSocket.Listen(10);                     // 設置最大連接數
31             Console.WriteLine("開始監聽");
32             Thread thread = new Thread(ListenClientConnect);        // 開啟線程監聽客戶端連接
33             thread.Start();
34             Console.ReadKey();
35         }
36 
37         /// <summary>
38         /// 監聽客戶端連接
39         /// </summary>
40         private static void ListenClientConnect()
41         {
42             Socket clientSocket = severSocket.Accept();         // 接收客戶端連接
43             Console.WriteLine("客戶端連接成功 信息: " + clientSocket.AddressFamily.ToString());
44             clientSocket.Send(Encoding.Default.GetBytes("你連接成功了"));
45             Thread revThread = new Thread(ReceiveClientManage);
46             revThread.Start(clientSocket);
47         }
48 
49         private static void ReceiveClientManage(object clientSocket)
50         {
51             Socket socket = clientSocket as Socket;
52             byte[] buffer = new byte[1024];
53             int length = socket.Receive(buffer);        // 從客戶端接收消息
54             Console.WriteLine("收到消息:" + Encoding.Default.GetString(buffer));
55         }
56     }
57 }

 

 

2. 客戶端代碼

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Net;
 5 using System.Net.Sockets;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 
 9 namespace client
10 {
11     /// <summary>
12     /// 客戶端程序
13     /// 1. 新建 Socket
14     /// 2. 連接服務器
15     /// 3. 接收服務器信息
16     /// 4. 向服務器發送信息
17     /// </summary>
18     class Program
19     {
20         private static Socket clientSocket = null;
21 
22         static void Main(string[] args)
23         {
24             clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
25             // 客戶端不需要綁定, 需要連接
26             IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);   // 端口號要與服務器對應
27             clientSocket.Connect(endPoint);
28             Console.WriteLine("連接到服務器");
29             // 接收服務器信息
30             byte[] buffer = new byte[1024];
31             int length = clientSocket.Receive(buffer);
32             Console.WriteLine("收到消息: " + Encoding.Default.GetString(buffer));
33             // 向服務器發送消息
34             clientSocket.Send(Encoding.Default.GetBytes("你好服務器"));
35             Console.ReadKey();
36         }
37     }
38 }

 

 

3. 運行截圖

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM