Chat Client/Server Part 1: Network Programming Primer and Packet Base Class
Let’s first expound on our example, beginning with its current state. Let’s say our wall in which our electrical socket resides is only capable of holding a single electrical port, and we now have housecleaning staff and multiple vacuum cleaners who need electricity to do their jobs. One member of the staff plugs in their vacuum cleaner, and goes about doing their work. A second staff member comes to the wall, and they will have to stand there holding their vacuum’s plug waiting because the socket is already being used by the first staff member. Once the first staff member has completed their work, they unplug and the second staffer can then plug into the socket.
As you can imagine, this would be terribly inefficient. Lets turn this around and discuss the way it should be implemented.
A staffer walks up to the electrical plug and plugs an electrical meter into the socket. That electrical meter reads out a number for the staff member, which is the room he or she is supposed to vacuum. The staff member walks into that room, and in that room is a totally different wall socket. The staff member plugs in and begins the work of vacuuming that room. A second staff member walks up to the main socket. He or she then inserts an electrical meter, and is given a different numbered room in which to work. That staff member walks into the different room, plugs into the socket there, and begins to vacuum. A third, fourth, and fifth member come up. Each is given a different number, and each work in different rooms.
In this example, no staff member is waiting on any other member except for the minuscule amount of time it takes for the electrical meter to tell the currently plugged in staffer which room he is assigned. Lets take this process and lay it out into technical terms.
- The main socket sits, listening for an incoming connection.
- When a connection is made, a separate socket is created on the server and the connection is handed off to that second socket. The connection is not interrupted during this process. The operating system automatically chooses a port number that is not being used for the new socket, and the connection is informed of the new destination port number.
- The main socket again begins listening for incoming connections.
- Each individual socket that is created during this process can send and receive data to their assigned client.
- When any client disconnects, the socket it was connected to is released and deleted by the server.
Here we have an efficient system! We could have thousands of clients connecting, and the server would handle them gracefully. Each client would only have to know one port number: the port number of the main listening socket. This is very manageable.
Follow Dave on Twitter

[...] See the original post here: Program With Dave » Blog Archive » Chat Client/Server Part 1 … [...]
[...] the original post: Program With Dave » Blog Archive » Chat Client/Server Part 1 … By admin | category: chat software | tags: dot-net, manifest-itself, p2p-video, result, [...]
[...] Address Already In Use: JVM_Bind:8080″. Now, if you’ve read my sockets tutorial network primer, you know that a port can only be bound for listening once. So apparently I had another process on [...]
[...] you have not read Part 1 of this Multi-Post tutorial, check it out now. This tutorial will rely on the code written in that [...]
[...] you have not read Part 1 and Part 2 of this network tutorial series, please do so now. The code in this post will build upon [...]