diff --git a/TCP/Example1/server.cs b/TCP/Example1/server.cs index 0ac039b..674022a 100644 --- a/TCP/Example1/server.cs +++ b/TCP/Example1/server.cs @@ -31,9 +31,9 @@ namespace PongExample1 public Server(IPAddress ip, int port = 5000) { server = new TcpListener(ip, port); - server.Start(); + server.Start(); // Start listening on given IP and port Console.WriteLine("Listening on {0} on port {1}", ip.ToString(), port); - s = server.AcceptSocket(); + s = server.AcceptSocket(); // Accept the connection as a socket into s } public Server(int port = 5000) : this(IPAddress.Loopback, port) { } @@ -44,10 +44,10 @@ namespace PongExample1 byte[] buffer = new byte[1024]; byte[] pong = "pong".ToByteArray(); - int k = s.Receive(buffer); + int k = s.Receive(buffer); // receive data from client Console.WriteLine("Client sent - {0} - {1}", buffer.GetString(k), count); Thread.Sleep(2000); - s.Send(pong); + s.Send(pong); // send data to client Console.WriteLine("Sent to client - {0} - {1}", pong.GetString(pong.Length), count); count++; return true; diff --git a/TCP/Example2/server.cs b/TCP/Example2/server.cs index 35cdfe1..08438f5 100644 --- a/TCP/Example2/server.cs +++ b/TCP/Example2/server.cs @@ -37,6 +37,9 @@ namespace PongExample1 while (true) { s = server.AcceptSocket(); + // spin up a new thread that handles + // incoming data from that client + // then loop to accept another connection Thread t = new Thread(Respond); t.Start(s); } diff --git a/TCP/Example3/server.cs b/TCP/Example3/server.cs index 592f2c4..85946f8 100644 --- a/TCP/Example3/server.cs +++ b/TCP/Example3/server.cs @@ -26,6 +26,9 @@ namespace ClientExample11 { Thread t = new Thread(handleConnection); Socket client = server.AcceptSocket(); + // Send the socket to the thread + // let the thread deal with incoming messages + // loop and accept the next connection and repeat t.Start(client); } @@ -39,6 +42,7 @@ namespace ClientExample11 } static void handleConnection(object socket) { + // unbox/cast socket object type to a Socket type Socket client = (Socket)socket; Console.WriteLine("Connection accepted from {0}", client.RemoteEndPoint);