Mars,
I have been reviewing your code and hope that this reply will help you out. In my reading and experience working with networking code there are basically two techniques to implement the code.
- Client/Server
- Peer-To-Peer
In the first model you create a server who has one thread dedicated to "accepting" new client connections. These connections all come in on the "listening" port, but typically, the client provides some random port (ephemeral). Once accepted, the server will typically spawn a "client thread" to handle communicating to the client. The client code is usually pretty simple, a single "connect" call to the server IP address and port and then uses that "socket" to send and receive data from the server.
Another way to implement a client/server model is to have the server poll the incoming "socket" for client activity. If activity is found, then it processes the activity quickly and then returns to polling again. To do this, you must enable "non-blocking" sockets on the listening socket you create for the server. The client code is the same as in the previous implementation, but you can also cause the client to poll for incoming data from the server in a non-blocking way by enabling "non-blocking" sockets for the client too.
In the Peer-To-Peer model you create multiple socket connections to all of your "peer" friends, there is no centralized server from which everyone tries to communicate. Another way to implement the Peer-To-Peer model is to use "broadcast" packets which get blasted out to anyone on the _same_ network (in other words, this is what many LAN games use, but doesn't work over the internet). This model is less centralized.
Now, getting back to your code. In your Server() method you "listen" and "accept" a connection, and your socket will block until you "accept" your first client. At which point it will exit and run the "DoStuff" in a separate thread. The problem is that you stop "accepting" any new clients. Therefore you can only have 1 client and 1 server at a time. It would be better if you made the "server" method into a thread and while loop so it could "accept" as many clients as you wanted. Each client that was "accepted" would spawn another thread to process sending data to that client.
Thread 1: Run while loop as server to listen and accept client connections
Thread 2: Run while loop accepting local input from the keyboard and posting this input to the client threads below.
Thread 3..n: Run while loop receiving data from the socket provided by the Accept call. This is the thread that will be spawned by Thread 1 above. Monitor a shared "broadcast" string to be sent to the client when Thread 2 posts to this shared "broadcast" string. Print any "string" messages received from the client socket to which this thread is responsible for.
I hope these explanations help, please write back soon with additional questions.