/// <summary> /// Setup the callback for recieved data and loss of conneciton /// </summary> /// <param name="app"></param> public void SetupRecieveCallback( AppMain app ) { try { AsyncCallback recieveData = new AsyncCallback(app.OnRecievedData); m_sock.BeginReceive( m_byBuff, 0, m_byBuff.Length, SocketFlags.None, recieveData, this ); } catch( Exception ex ) { Console.WriteLine( "Recieve callback setup failed! {0}", ex.Message ); } }
/// <summary> /// Application starts here. Create an instance of this class and use it /// as the main object. /// </summary> /// <param name="args"></param> static void Main(string[] args) { AppMain app = new AppMain(); // Welcome and Start listening Console.WriteLine( "*** Chat Server Started {0} *** ", DateTime.Now.ToString( "G" ) ); /* // // Method 1 // Socket client; const int nPortListen = 399; try { TcpListener listener = new TcpListener( nPortListen ); Console.WriteLine( "Listening as {0}", listener.LocalEndpoint ); listener.Start(); do { byte [] m_byBuff = new byte[127]; if( listener.Pending() ) { client = listener.AcceptSocket(); // Get current date and time. DateTime now = DateTime.Now; String strDateLine = "Welcome " + now.ToString("G") + "\n\r"; // Convert to byte array and send. Byte[] byteDateLine = System.Text.Encoding.ASCII.GetBytes( strDateLine.ToCharArray() ); client.Send( byteDateLine, byteDateLine.Length, 0 ); } else { Thread.Sleep( 100 ); } } while( true ); // Don't use this. //Console.WriteLine ("OK that does it! Screw you guys I'm going home..." ); //listener.Stop(); } catch( Exception ex ) { Console.WriteLine ( ex.Message ); } */ // // Method 2 // const int nPortListen = 399; // Determine the IPAddress of this machine IPAddress [] aryLocalAddr = null; String strHostName = ""; try { // NOTE: DNS lookups are nice and all but quite time consuming. strHostName = Dns.GetHostName(); IPHostEntry ipEntry = Dns.GetHostByName( strHostName ); aryLocalAddr = ipEntry.AddressList; } catch( Exception ex ) { Console.WriteLine ("Error trying to get local address {0} ", ex.Message ); } // Verify we got an IP address. Tell the user if we did if( aryLocalAddr == null || aryLocalAddr.Length < 1 ) { Console.WriteLine( "Unable to get local address" ); return; } Console.WriteLine( "Listening on : [{0}] {1}:{2}", strHostName, aryLocalAddr[0], nPortListen ); // Create the listener socket in this machines IP address Socket listener = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); listener.Bind( new IPEndPoint( aryLocalAddr[0], 399 ) ); //listener.Bind( new IPEndPoint( IPAddress.Loopback, 399 ) ); // For use with localhost 127.0.0.1 listener.Listen( 10 ); // Setup a callback to be notified of connection requests listener.BeginAccept( new AsyncCallback( app.OnConnectRequest ), listener ); Console.WriteLine ("Press Enter to exit" ); Console.ReadLine(); Console.WriteLine ("OK that does it! Screw you guys I'm going home..." ); // Clean up before we go home listener.Close(); GC.Collect(); GC.WaitForPendingFinalizers(); }
private ArrayList m_aryClients = new ArrayList(); // List of Client Connections /// <summary> /// Application starts here. Create an instance of this class and use it /// as the main object. /// </summary> /// <param name="args"></param> static void Main(string[] args) { AppMain app = new AppMain(); // Welcome and Start listening Console.WriteLine("*** Chat Server Started {0} *** ", DateTime.Now.ToString("G")); /* * // * // Method 1 * // * Socket client; * const int nPortListen = 399; * try * { * TcpListener listener = new TcpListener( nPortListen ); * Console.WriteLine( "Listening as {0}", listener.LocalEndpoint ); * listener.Start(); * do * { * byte [] m_byBuff = new byte[127]; * if( listener.Pending() ) * { * client = listener.AcceptSocket(); * // Get current date and time. * DateTime now = DateTime.Now; * String strDateLine = "Welcome " + now.ToString("G") + "\n\r"; * * // Convert to byte array and send. * Byte[] byteDateLine = System.Text.Encoding.ASCII.GetBytes( strDateLine.ToCharArray() ); * client.Send( byteDateLine, byteDateLine.Length, 0 ); * } * else * { * Thread.Sleep( 100 ); * } * } while( true ); // Don't use this. * * //Console.WriteLine ("OK that does it! Screw you guys I'm going home..." ); * //listener.Stop(); * } * catch( Exception ex ) * { * Console.WriteLine ( ex.Message ); * } */ // // Method 2 // const int nPortListen = 9000; // Determine the IPAddress of this machine IPAddress [] aryLocalAddr = null; String strHostName = ""; try { // NOTE: DNS lookups are nice and all but quite time consuming. strHostName = Dns.GetHostName(); IPHostEntry ipEntry = Dns.GetHostEntry(strHostName); aryLocalAddr = ipEntry.AddressList; } catch (Exception ex) { Console.WriteLine("Error trying to get local address {0} ", ex.Message); } // Verify we got an IP address. Tell the user if we did if (aryLocalAddr == null || aryLocalAddr.Length < 1) { Console.WriteLine("Unable to get local address"); return; } Console.WriteLine("Listening on : [{0}] {1}:{2}", strHostName, aryLocalAddr[0], nPortListen); // Create the listener socket in this machines IP address Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listener.Bind(new IPEndPoint(aryLocalAddr[0], 399)); //listener.Bind( new IPEndPoint( IPAddress.Loopback, 399 ) ); // For use with localhost 127.0.0.1 listener.Listen(10); // Setup a callback to be notified of connection requests listener.BeginAccept(new AsyncCallback(app.OnConnectRequest), listener); Console.WriteLine("Press Enter to exit"); Console.ReadLine(); Console.WriteLine("OK that does it! Screw you guys I'm going home..."); // Clean up before we go home listener.Close(); GC.Collect(); GC.WaitForPendingFinalizers(); }