Advertisement
Guest User

Untitled

a guest
May 10th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5.  
  6. namespace Client
  7. {
  8.     public class SynchronousSocketClient
  9.     {
  10.  
  11.         public static void StartClient()
  12.         {
  13.             // Data buffer for incoming data.
  14.             byte[] bytes = new byte[1024];
  15.  
  16.             Console.WriteLine("Please, set the remote address");
  17.             var address = Console.ReadLine();
  18.  
  19.             if (address == null) return;
  20.             try
  21.             {
  22.                 IPHostEntry ipHostInfo = Dns.Resolve(address);
  23.                 IPAddress ipAddress = ipHostInfo.AddressList[0];
  24.                 IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11010);
  25.  
  26.                 Socket sender = new Socket(AddressFamily.InterNetwork,
  27.                                            SocketType.Stream, ProtocolType.Tcp);
  28.  
  29.                 // Connect the socket to the remote endpoint. Catch any errors.
  30.                 try
  31.                 {
  32.                     sender.Connect(remoteEP);
  33.  
  34.                     Console.WriteLine("Socket connected to {0}",
  35.                                       sender.RemoteEndPoint.ToString());
  36.                     Console.WriteLine("input your message");
  37.                     var message = Console.ReadLine();
  38.                     // Encode the data string into a byte array.
  39.                     byte[] msg = Encoding.ASCII.GetBytes(message + "<EOF>");
  40.  
  41.                     // Send the data through the socket.
  42.                     int bytesSent = sender.Send(msg);
  43.  
  44.                     // Receive the response from the remote device.
  45.                     int bytesRec = sender.Receive(bytes);
  46.                     Console.WriteLine("Echoed test = {0}",
  47.                                       Encoding.ASCII.GetString(bytes, 0, bytesRec));
  48.  
  49.                     // Release the socket.
  50.                     sender.Shutdown(SocketShutdown.Both);
  51.                     sender.Close();
  52.  
  53.                 }
  54.                 catch (ArgumentNullException ane)
  55.                 {
  56.                     Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
  57.                 }
  58.                 catch (SocketException se)
  59.                 {
  60.                     Console.WriteLine("SocketException : {0}", se.ToString());
  61.                 }
  62.                 catch (Exception e)
  63.                 {
  64.                     Console.WriteLine("Unexpected exception : {0}", e.ToString());
  65.                 }
  66.  
  67.             }
  68.             catch (Exception e)
  69.             {
  70.                 Console.WriteLine(e.ToString());
  71.             }
  72.         }
  73.  
  74.         public static int Main(String[] args)
  75.         {
  76.             StartClient();
  77.             return 0;
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement