C# TCP服务端 可以接收其他语言或平台发送的TCP消息

来源:互联网 发布:mac程序卸载后图标还在 编辑:程序博客网 时间:2024/06/04 08:49

  1. class Program  
  2.     {  
  3.         public static void Main()  
  4.         {  
  5.             TcpListener server = null;  
  6.             try  
  7.             {  
  8.                 // Set the TcpListener on port 13000.  
  9.                 Int32 port = 8000;  
  10.                 IPAddress localAddr = IPAddress.Any;  
  11.   
  12.                 // TcpListener server = new TcpListener(port);  
  13.                 server = new TcpListener(localAddr, port);  
  14.   
  15.                 // Start listening for client requests.  
  16.                 server.Start();  
  17.   
  18.                 // Buffer for reading data  
  19.                 Byte[] bytes = new Byte[4096];  
  20.                 String data = null;  
  21.   
  22.                 // Enter the listening loop.  
  23.                 while (true)  
  24.                 {  
  25.                     Console.Write("Waiting for a connection... ");  
  26.   
  27.                     // Perform a blocking call to accept requests.  
  28.                     // You could also user server.AcceptSocket() here.  
  29.                     TcpClient client = server.AcceptTcpClient();  
  30.                     Console.WriteLine("Connected!");  
  31.   
  32.                     data = null;  
  33.   
  34.                     // Get a stream object for reading and writing  
  35.                     NetworkStream stream = client.GetStream();  
  36.   
  37.                     int i;  
  38.   
  39.                     // Loop to receive all the data sent by the client.  
  40.                     while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)  
  41.                     {  
  42.                         // Translate data bytes to a ASCII string.  
  43.                         data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);  
  44.                         Console.WriteLine("Received: {0}", data);  
  45.   
  46.                         // Process the data sent by the client.  
  47.                         data = data.ToUpper();  
  48.   
  49.                         byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);  
  50.   
  51.                         // Send back a response.  
  52.                         stream.Write(msg, 0, msg.Length);  
  53.                         Console.WriteLine("Sent: {0}", data);  
  54.                     }  
  55.   
  56.                     // Shutdown and end connection  
  57.                     client.Close();  
  58.                 }  
  59.             }  
  60.             catch (SocketException e)  
  61.             {  
  62.                 Console.WriteLine("SocketException: {0}", e);  
  63.             }  
  64.             finally  
  65.             {  
  66.                 // Stop listening for new clients.  
  67.                 server.Stop();  
  68.             }  
  69.   
  70.   
  71.             Console.WriteLine("\nHit enter to continue...");  
  72.             Console.Read();  
  73.         }  
  74.     }  

转自:http://blog.csdn.net/educast/article/details/7386917

原创粉丝点击