线程传参
代码
1 private void StartListening()
2 {
3
4 try
5 {
6 while (true)
7 {
8
9 Socket ClientSocket =server.Accept();
10 Thread ThreadClientService = new Thread(new ParameterizedThreadStart(ServiceClient));
11 ThreadClientService.IsBackground = true;
12 ThreadClientService.Start(ClientSocket);
13 }
14 }
15 catch
16 {
17 StatusContentLabel.Text = "终端不在线!";
18 }
19 }
20
21 // 当服务器端接收到一个来自客户端的连接请求后,它就打开一个ServiceClient()线程来服务客户端。
22 private void ServiceClient(object socket)
23 {
24 Socket client = (Socket)socket;
25 bool keepalive = true; //各个客户Socket线程存活的标识
26
27 while (keepalive)
28 {
29 byte[] DataFromSingleDTU=new byte[128];
30 try
31 {
32 client.Receive(DataFromSingleDTU, DataFromSingleDTU.Length, SocketFlags.None);
33 }
34 }
35 }
36