.net下单线程同步模式

来源:互联网 发布:聚合视频软件 编辑:程序博客网 时间:2024/05/18 15:51

 摘自msdn

 

客户端:

  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. public class SynchronousSocketClient {
  6.     public static void StartClient() {
  7.         // Data buffer for incoming data.
  8.         byte[] bytes = new byte[1024];
  9.         // Connect to a remote device.
  10.         try {
  11.             // Establish the remote endpoint for the socket.
  12.             // This example uses port 11000 on the local computer.
  13.             IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName())
  14.             IPAddress ipAddress = ipHostInfo.AddressList[0];
  15.             IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000);
  16.             // Create a TCP/IP  socket.
  17.             Socket sender = new Socket(AddressFamily.InterNetwork, 
  18.                 SocketType.Stream, ProtocolType.Tcp );
  19.             // Connect the socket to the remote endpoint. Catch any errors.
  20.             try {
  21.                 sender.Connect(remoteEP);
  22.                 Console.WriteLine("Socket connected to {0}",
  23.                     sender.RemoteEndPoint.ToString());
  24.                 // Encode the data string into a byte array.
  25.                 byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");
  26.                 // Send the data through the socket.
  27.                 int bytesSent = sender.Send(msg);
  28.                 // Receive the response from the remote device.
  29.                 int bytesRec = sender.Receive(bytes);
  30.                 Console.WriteLine("Echoed test = {0}",
  31.                     Encoding.ASCII.GetString(bytes,0,bytesRec));
  32.                 // Release the socket.
  33.                 sender.Shutdown(SocketShutdown.Both);
  34.                 sender.Close();
  35.                 
  36.             } catch (ArgumentNullException ane) {
  37.                 Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
  38.             } catch (SocketException se) {
  39.                 Console.WriteLine("SocketException : {0}",se.ToString());
  40.             } catch (Exception e) {
  41.                 Console.WriteLine("Unexpected exception : {0}", e.ToString());
  42.             }
  43.         } catch (Exception e) {
  44.             Console.WriteLine( e.ToString());
  45.         }
  46.     }
  47.     
  48.     public static int Main(String[] args) {
  49.         StartClient();
  50.         return 0;
  51.     }
  52. }

服务器端:

  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. public class SynchronousSocketListener {
  6.     
  7.     // Incoming data from the client.
  8.     public static string data = null;
  9.     public static void StartListening() {
  10.         // Data buffer for incoming data.
  11.         byte[] bytes = new Byte[1024];
  12.         // Establish the local endpoint for the socket.
  13.         // Dns.GetHostName returns the name of the 
  14.         // host running the application.
  15.         IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
  16.         IPAddress ipAddress = ipHostInfo.AddressList[0];
  17.         IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
  18.         // Create a TCP/IP socket.
  19.         Socket listener = new Socket(AddressFamily.InterNetwork,
  20.             SocketType.Stream, ProtocolType.Tcp );
  21.         // Bind the socket to the local endpoint and 
  22.         // listen for incoming connections.
  23.         //启动一个网络进程守候 程序,以后送往该IP以及该端口的数据 全部由该进程守候程序处理
  24.         try {
  25.             listener.Bind(localEndPoint);
  26.             listener.Listen(10);
  27.             // Start listening for connections.
  28.             //每一次处理一个tcp连接。
  29.             while (true) {
  30.                 Console.WriteLine("Waiting for a connection...");
  31.                 // Program is suspended while waiting for an incoming connection.
  32.                 Socket handler = listener.Accept();
  33.                 data = null;
  34.                 // An incoming connection needs to be processed.
  35.                 while (true) {
  36.                     bytes = new byte[1024];
  37.                     int bytesRec = handler.Receive(bytes);
  38.                     data += Encoding.ASCII.GetString(bytes,0,bytesRec);
  39.                     //服务器判断不出数据有没有传完,需要由用户自己定义数据的末尾。
  40.                     if (data.IndexOf("<EOF>") > -1) {
  41.                         break;
  42.                     }
  43.                 }
  44.                 // Show the data on the console.
  45.                 Console.WriteLine( "Text received : {0}", data);
  46.                 // Echo the data back to the client.
  47.                 byte[] msg = Encoding.ASCII.GetBytes(data);
  48.                 handler.Send(msg);
  49.                 handler.Shutdown(SocketShutdown.Both);
  50.                 handler.Close();
  51.             }
  52.             
  53.         } catch (Exception e) {
  54.             Console.WriteLine(e.ToString());
  55.         }
  56.         Console.WriteLine("/nPress ENTER to continue...");
  57.         Console.Read();
  58.         
  59.     }
  60.     public static int Main(String[] args) {
  61.         StartListening();
  62.         return 0;
  63.     }
  64. }
  65. 在这种模式中如果socket中,如果绑定的IP以及端口没有“待处理”的数据,会使处理函数处于等待状态而无法返回。
原创粉丝点击