日期:2014-05-18 浏览次数:20904 次
IPAddress ip = IPAddress.Parse("127.0.0.1"); IPEndPoint iep = new IPEndPoint(ip, 2012); Socket mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); mySocket.Bind(iep); mySocket.Listen(10); Socket myClient = mySocket.Accept(); myClient.Send(System.Text.Encoding.Unicode.GetBytes("连接成功")); byte[] buf = new byte[1024]; int k = myClient.Receive(buf); System.Console.WriteLine(System.Text.Encoding.Unicode.GetString(buf, 0, k)); myClient.Send(System.Text.Encoding.Unicode.GetBytes("客户你好")); myClient.Shutdown(SocketShutdown.Both); myClient.Close(); mySocket.Close();