public static void ListenConnection() { Socket ConnectionSocket = null; while (true) { try { ConnectionSocket = ServerSocket.Accept(); } catch (Exception ex) { Console.WriteLine("监听套接字异常{0}", ex.Message); break; } //获取客户端端口号和IP IPAddress ClientIP = (ConnectionSocket.RemoteEndPoint as IPEndPoint).Address; int ClientPort = (ConnectionSocket.RemoteEndPoint as IPEndPoint).Port; string SendMessage = "连接服务器成功\r\n" + "本地IP:" + ClientIP + ",本地端口:" + ClientPort.ToString(); //Console.WriteLine(SendMessage); ConnectionSocket.Send(Encoding.Unicode.GetBytes(SendMessage)); string remotePoint = ConnectionSocket.RemoteEndPoint.ToString(); Console.WriteLine("成功与客户端{0}建立连接!\t\n", remotePoint); ClientInformation.Add(remotePoint, ConnectionSocket); Thread thread = new Thread(ReceiveMessage); thread.IsBackground = true; thread.Start(ConnectionSocket); } }