.net下单线程同步模式(2)

 在.net中采用TcpListener、TcpClient代替 socket实现单线程同步模式

 

 

服务器端:

  1. static void Connect(String server, String message) 
  2. {
  3.   try 
  4.   {
  5.     // Create a TcpClient.
  6.     // Note, for this client to work you need to have a TcpServer 
  7.     // connected to the same address as specified by the server, port
  8.     // combination.
  9.     Int32 port = 13000;
  10.     TcpClient client = new TcpClient(server, port);
  11.     // Translate the passed message into ASCII and store it as a Byte array.
  12.     Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);         
  13.     // Get a client stream for reading and writing.
  14.    //  Stream stream = client.GetStream();
  15.     NetworkStream stream = client.GetStream();
  16.     // Send the message to the connected TcpServer. 
  17.     stream.Write(data, 0, data.Length);
  18.     Console.WriteLine("Sent: {0}", message);         
  19.     // Receive the TcpServer.response.
  20.     // Buffer to store the response bytes.
  21.     data = new Byte[256];
  22.     // String to store the response ASCII representation.
  23.     String responseData = String.Empty;
  24.     // Read the first batch of the TcpServer response bytes.
  25.     Int32 bytes = stream.Read(data, 0, data.Length);
  26.     responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
  27.     Console.WriteLine("Received: {0}", responseData);         
  28.     // Close everything.
  29.     stream.Close();         
  30.     client.Close();         
  31.   } 
  32.   catch (ArgumentNullException e) 
  33.   {
  34.     Console.WriteLine("ArgumentNullException: {0}", e);
  35.   } 
  36.   catch (SocketException e) 
  37.   {
  38.     Console.WriteLine("SocketException: {0}", e);
  39.   }
  40.   Console.WriteLine("/n Press Enter to continue...");
  41.   Console.Read();
  42. }

 

  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. class MyTcpListener
  7. {
  8.   public static void Main()
  9.   { 
  10.     TcpListener server=null;   
  11.     try
  12.     {
  13.       // Set the TcpListener on port 13000.
  14.       Int32 port = 13000;
  15.       IPAddress localAddr = IPAddress.Parse("127.0.0.1");
  16.       // TcpListener server = new TcpListener(port);
  17.       server = new TcpListener(localAddr, port);
  18.       // Start listening for client requests.
  19.       server.Start();
  20.       // Buffer for reading data
  21.       Byte[] bytes = new Byte[256];
  22.       String data = null;
  23.       // Enter the listening loop.
  24.       while(true
  25.       {
  26.         Console.Write("Waiting for a connection... ");
  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.         data = null;
  32.         // Get a stream object for reading and writing
  33.         NetworkStream stream = client.GetStream();
  34.         int i;
  35.         // Loop to receive all the data sent by the client.
  36.         while((i = stream.Read(bytes, 0, bytes.Length))!=0) 
  37.         {   
  38.           // Translate data bytes to a ASCII string.
  39.           data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
  40.           Console.WriteLine("Received: {0}", data);
  41.           // Process the data sent by the client.
  42.           data = data.ToUpper();
  43.           byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
  44.           // Send back a response.
  45.           stream.Write(msg, 0, msg.Length);
  46.           Console.WriteLine("Sent: {0}", data);            
  47.         }
  48.         // Shutdown and end connection
  49.         client.Close();
  50.       }
  51.     }
  52.     catch(SocketException e)
  53.     {
  54.       Console.WriteLine("SocketException: {0}", e);
  55.     }
  56.     finally
  57.     {
  58.        // Stop listening for new clients.
  59.        server.Stop();
  60.     }
  61.     Console.WriteLine("/nHit enter to continue...");
  62.     Console.Read();
  63.   }   

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值