public static void Main() { IPAddress myIP = TCPUtils.GetIPV4FromHostName(hostName); TcpListener myList; // In this outer block, if we have an error we are done. try { /* Initializes the Listener */ myList = new TcpListener(myIP, hostPort); /* Start Listening at the specified port */ myList.Start(); Boolean connected = false; while (!connected) { Console.WriteLine("TCP server local end point: " + myList.LocalEndpoint); Console.WriteLine("Waiting for a connection....."); Socket s = myList.AcceptSocket(); Console.WriteLine("Connection accepted from " + s.RemoteEndPoint); connected = true; // DateTime connectionStartTime = DateTime.Now; // while (TCPUtils.ElapsedSecondsSince(timer) < sockTimeout) while (connected) { byte[] b = new byte[recvBufSize]; try { int k = s.Receive(b); Console.WriteLine("\nReceived..."); for (int i = 0; i < k; i++) { Console.Write(Convert.ToChar(b[i])); } ASCIIEncoding asen = new ASCIIEncoding(); s.Send(asen.GetBytes("The string was received by the server.")); Console.WriteLine("\nSent Acknowledgement"); } catch (SocketException e) { Console.WriteLine("Exception: " + e.ToString()); // Console.WriteLine("Error..... " + e.StackTrace); s.Close(); connected = false; } } } /* clean up */ myList.Stop(); } catch (Exception e) { Console.WriteLine("Exception: " + e.ToString()); // Console.WriteLine("Error..... " + e.StackTrace); } } // end Main
public static void StartListening() { // Data buffer for incoming data. byte[] bytes = new Byte[1024]; // Establish the local endpoint for the socket. //IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); //IPAddress ipAddress = ipHostInfo.AddressList[0]; //IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); //Console.WriteLine("TCP server local end point: " + localEndPoint); IPAddress ipAddress = TCPUtils.GetIPV4FromHostName(hostName); IPEndPoint localEndPoint = new IPEndPoint(ipAddress, hostPort); Console.WriteLine("TCP server local end point: " + localEndPoint); // Create a TCP/IP socket. Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local endpoint and listen for incoming connections. try { listener.Bind(localEndPoint); listener.Listen(100); while (true) { // Set the event to nonsignaled state. allDone.Reset(); // Start an asynchronous socket to listen for connections. Console.WriteLine("Waiting for a connection..."); listener.BeginAccept( new AsyncCallback(AcceptCallback), listener); // Wait until a connection is made before continuing. allDone.WaitOne(); // Console.WriteLine("allDone.WaitOne() complete"); } } catch (Exception e) { Console.WriteLine(e.ToString()); } Console.WriteLine("\nStartListening done. Press ENTER to continue..."); Console.Read(); }
// private static System.Timers.Timer aTimer; // private static void SetTimer() // { // // Create a timer with a two second interval. // aTimer = new System.Timers.Timer(10000); // // Hook up the Elapsed event for the timer. // aTimer.Elapsed += OnTimedEvent; // aTimer.AutoReset = true; // aTimer.Enabled = true; // } // private static void OnTimedEvent(Object source, ElapsedEventArgs e) // { // Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}", // e.SignalTime); //} public static void Main() { IPAddress myIP = TCPUtils.GetIPV4FromHostName(hostName); // // Start our timer which will interrupt us every ten seconds. // SetTimer(); try { // Outer loop: we enter this loop without an instantiated TCPClient object. If we get back to the // top of the loop again it's because the client has been closed and we need to start it up again while (true) { TcpClient tcpclnt = new TcpClient(); // Inner try clause. We want to intercept socket connection failures, try again after // socktimeout seconds while (!tcpclnt.Connected) { try { Console.WriteLine("Connecting to {0:S} on port {1:D}...", hostName, hostPort); //Console.WriteLine("Connecting..."); tcpclnt.Connect(myIP, hostPort); } // Inner catch clause. We want to intercept socket connection failures here. // But we are only catching socket exceptions; anything else will blow up the program catch (SocketException e) { Console.WriteLine("Inner SocketException 1: " + e.ToString()); // Console.WriteLine("Exception: " + e.GetType().ToString()); // Console.WriteLine("Error..... " + e.StackTrace); } if (!tcpclnt.Connected) { // Wait "socktimeout" seconds and then we'll try again DateTime timer = DateTime.Now; while (TCPUtils.ElapsedSecondsSince(timer) < sockTimeout) { // elapsed = TCPUtils.ElapsedSecondsSince(timer); // Console.WriteLine("Connect retry loop: elapsed seconds: " + elapsed.ToString()); Thread.Sleep(1000); } } else { Console.WriteLine("Connected"); } } // while (!tcpclnt.connected) // Loop until a null string is entered while (true) { Console.Write("Enter the string to be transmitted : "); String str = Console.ReadLine(); if (str.Length == 0) { break; } // add back the terminating newline that we'll use as end-of-record separator str = str + "\n"; // Try clause for writing to and reading from the socket. // Catch exceptions like other end closed try { Stream stm = tcpclnt.GetStream(); ASCIIEncoding asen = new ASCIIEncoding(); // HACK: Add a prefix with no trailing record separator string prefix = ":prefix:"; byte[] ba = asen.GetBytes(prefix); Console.WriteLine("Transmitting....."); stm.Write(ba, 0, ba.Length); // Now send the terminated string //byte[] ba = asen.GetBytes(str); ba = asen.GetBytes(str); Console.WriteLine("Transmitting....."); stm.Write(ba, 0, ba.Length); // byte[] bb = new byte[100]; byte[] bb = new byte[100]; int k = stm.Read(bb, 0, 100); for (int i = 0; i < k; i++) { Console.Write(Convert.ToChar(bb[i])); } Console.WriteLine(""); } catch (Exception e) { Console.WriteLine("Inner Exception 2: " + e.ToString()); Console.WriteLine("Exception Type: " + e.GetType().ToString()); tcpclnt.Close(); break; } } Console.WriteLine("Broke out of loop. Closing TCP client."); tcpclnt.Close(); } // while (true() } // We can have multiple catch clauses tailored to specific error conditions. These are all // redundant because they don't do anything specific to the type of exception being caught. catch (SocketException e) { Console.WriteLine("Outer Socket Exception: " + e.ToString()); Console.WriteLine("Exception: " + e.GetType().ToString()); } catch (IOException e) { Console.WriteLine("Outer IO Exception: " + e.ToString()); Console.WriteLine("Exception: " + e.GetType().ToString()); } catch (Exception e) { Console.WriteLine("Outer General Exception: " + e.ToString()); Console.WriteLine("Exception: " + e.GetType().ToString()); } // // Clean up our timer // aTimer.Stop(); // aTimer.Dispose(); } // end public static void Main()