private void ReceiveCmdTCP() { IPAddress local = IPAddress.Any; IPEndPoint iep = new IPEndPoint(local, _localport); Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); server.Bind(iep); server.Listen(50); // CMDOperator CMD = new CMDOperator(mainmap, LayoutRoot, this); while (true) // 等待TCP连接 { try { Socket client = server.Accept(); // 同步方式等待客户端接入 CMDOperator CMD = new CMDOperator(this); TcpThread newClient = new TcpThread(client, CMD); // Thread newThread = new Thread(new ThreadStart(newClient.ClientService)); // 为每个连接单独建一个线程处理命令 Task newThread = new Task(newClient.ClientService); newThread.Start(); } catch (Exception) { } } }
public TcpThread(Socket k, CMDOperator _cmd) { client = k; CMD = _cmd; }