Chat Client/Server Part 1: Network Programming Primer and Packet Base Class
This is the first part of a multiple-post tutorial which will explain how to use the dot net TcpClient and TcpListener classes to create a multi-threaded network application. This result will manifest itself in a chat server and chat client.
Basics of Networking
The full methodologies of network programming are a beyond the scope of this article, so I will just give a very brief and elementary overview of how network programming works. We will be writing our chat server and client using the TcpClient and TcpServer classes which are built into System.Net in the dot net framework. These classes themselves are built on top of System.Net.Sockets, which wraps the socket structure provided by the operating system.
A “socket” is the basic connector in network communication. A socket can send and receive data, and a socket can listen for incoming connections. Think of an electrical wall socket in your home. It is sitting there, waiting for a connection. We call waiting for a connection “listening.” When you plug in a vacuum cleaner, the socket comes alive and provides electricity (the metaphor for network data) to the device. Plugging in the device is called “connecting” to the socket. When you unplug the vacuum cleaner, the flow is interrupted and the vacuum cleaner no longer receives power. This unplugging is called “disconnecting.”
To put the above into technical terms, here is the flow of a socket’s use.
- The socket sits, listening for an incoming connection.
- When a connection is made, the socket is capable of sending and receiving data to the connected device.
- The client disconnects from the socket.
- The socket again begins listening for incoming connections.
The above is fine and dandy if you will ever only have a single vacuum cleaner. But what happens, as often does in the world of networking, when you have many devices that want to connect to the same server? The most basic answer is to create many sockets. That is the answer to the problem, but there is a catch to it which makes it a little more complicated. That catch is called “ports.” A port is a number in which your socket will be listening to. Only one socket can listen to any port on a server at any given time. If another socket attempts to listen on the same port, the operating system will throw an error. You may have heard of “port 80″ for website traffic, or “port 21″ for file transfer traffic. These are the numbers on which the server listens for incoming clients. It would not work to create many sockets listening on many ports, because your clients would never know what ports were already being used by other clients and what ports were not. So, how do you overcome this problem?
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 [...]