using System; using System.Net; using System.Net.Sockets; using System.Text; public class SocketClient { public static int Main(String[] args) { if (args.Length == 0) { Console.WriteLine("String.exe someIpAddress"); } else { StartClient(args[0]); } return 0; } public static void StartClient(String ipAddressArg) { byte[] bytes = new byte[1024]; try { IPAddress ipAddress = IPAddress.Parse(ipAddressArg); IPEndPoint remoteEP = new IPEndPoint(ipAddress, 1234); Socket sender = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); try { sender.Connect(remoteEP); Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString()); int bytesRec = sender.Receive(bytes); Console.WriteLine("{0}", Encoding.ASCII.GetString(bytes, 0, bytesRec)); sender.Shutdown(SocketShutdown.Both); sender.Close(); } catch (ArgumentNullException ane) { Console.WriteLine("ArgumentNullException : {0}", ane.ToString()); } catch (SocketException se) { Console.WriteLine("SocketException : {0}", se.ToString()); } catch (Exception e) { Console.WriteLine("Unexpected exception : {0}", e.ToString()); } } catch (Exception e) { Console.WriteLine(e.ToString()); } } }