同步客户端套接字示例

同步客户端套接字示例
    下面的示例程序创建一个连接到服务器的客户端。该客户端是用同步套接字生成的,因此挂起客户端应用程序的执行,直到服务器返回响应为止。该应用程序将字符串发送到服务器,然后在控制台显示该服务器返回的字符串。

 

  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.     } catch (ArgumentNullException ane) {
  36.     Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
  37.     } catch (SocketException se) {
  38.     Console.WriteLine("SocketException : {0}",se.ToString());
  39.     } catch (Exception e) {
  40.     Console.WriteLine("Unexpected exception : {0}", e.ToString());
  41.     }
  42.     } catch (Exception e) {
  43.     Console.WriteLine( e.ToString());
  44.     }
  45.     }
  46.     public static int Main(String[] args) {
  47.     StartClient();
  48.     return 0;
  49.     }
  50.     }

同步服务器套接字示例 下面的示例程序创建一个接收来自客户端的连接请求的服务器。该服务器是用同步套接字生成的,
    因此在等待来自客户端的连接时挂起服务器应用程序的执行。该应用程序接收来自客户端的字符串,
    在控制台显示该字符串,然后将该字符串回显到客户端。来自客户端的字符串必须包含字符串“<EOF>”,
    以发出表示消息结尾的信号。

  1. using system;
  2.     using system.Net;
  3.     using system.Net.Sockets;
  4.     using system.Text;
  5.     public class SynchronousSocketListener {
  6.     // Incoming data from the client.
  7.     public static string data = null;
  8.     public static void StartListening() {
  9.     // Data buffer for incoming data.
  10.     byte[] bytes = new Byte[1024];
  11.     // Establish the local endpoint for the socket.
  12.     // Dns.GetHostName returns the name of the
  13.     // host running the application.
  14.     IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
  15.     IPAddress ipAddress = ipHostInfo.AddressList[0];
  16.     IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
  17.     // Create a tcp/IP socket.
  18.     Socket listener = new Socket(AddressFamily.InterNetwork,
  19.     SocketType.Stream, ProtocolType.Tcp );
  20.     // Bind the socket to the local endpoint and
  21.     // listen for incoming connections.
  22.     try {
  23.     listener.Bind(localEndPoint);
  24.     listener.Listen(10);
  25.     // Start listening for connections.
  26.     while (true) {
  27.     Console.WriteLine("Waiting for a connection...");
  28.     // Program is suspended while waiting for an incoming connection.
  29.     Socket handler = listener.Accept();
  30.     data = null;
  31.     // An incoming connection needs to be processed.
  32.     while (true) {
  33.     bytes = new byte[1024];
  34.     int bytesRec = handler.Receive(bytes);
  35.     data += Encoding.ASCII.GetString(bytes,0,bytesRec);
  36.     if (data.IndexOf("<EOF>") > -1) {
  37.     break;
  38.     }
  39.     }
  40.     // Show the data on the console.
  41.     Console.WriteLine( "Text received : {0}", data);
  42.     // Echo the data back to the client.
  43.     byte[] msg = Encoding.ASCII.GetBytes(data);
  44.     handler.Send(msg);
  45.     handler.Shutdown(SocketShutdown.Both);
  46.     handler.Close();
  47.     }
  48.     } catch (Exception e) {
  49.     Console.WriteLine(e.ToString());
  50.     }
  51.     Console.WriteLine("/nPress ENTER to continue...");
  52.     Console.Read();
  53.     }
  54.     public static int Main(String[] args) {
  55.     StartListening();
  56.     return 0;
  57.     }
  58.     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值