Gigi Labs

Saturday, May 18, 2013

Network Programming: Networking Theory

Hi all! :)

In yesterday's article (C# Network Programming: Echo Client/Server), we saw a simple example of a client and server communicating together by means of a simple protocol. However, a lot of questions remained unanswered.

Today we're going to learn a bit more about how the internet actually works, and that will help understand network programming better.

Let's say we have the setting above: the laptop on the left is connected to the server on the right. As we have seen yesterday, the laptop (client) must know the server's IP address and port in order to connect to it. The IP address and port together form an endpoint or socket.

The client must also have an endpoint of its own, to form such a connection. But since it is a client, the port is assigned automatically by the operating system - which is why we normally don't see it in network programming.

A port can be anything between 0 and 65535, but the first 1024 are reserved for standard services (such as HTTP or email), so we normally use ports 1024 onwards for our custom programs. By using different ports, a single computer (i.e. a single IP address) may have several different incoming and outgoing connections at the same time.

The internet works a little bit like the postal system. If you want to send someone a letter, you normally put it in an envelope, and write the person's address on the envelope. The postal system will then find a way to deliver your letter. In the case of the internet, this happens mostly thanks to TCP/IP (TCP over IP). The IP protocol (which is where IP addresses come from) takes care of routing a message from one computer to another - it can pass through several other routers/servers on the way.

While IP can find a route between the sending and receiving computers, another protocol (TCP or UDP) must be used to deliver the message to the correct application on the destination computer (using ports). While TCP is normally preferred because it allows reliable message delivery, UDP is simpler and useful in certain applications (e.g. video streaming, where the loss of a little bit of data is better than waiting for it to be retransmitted).

IP, TCP and UDP are part of a bigger picture called the OSI model, which categorises internet protocols. It looks something like this:

OSI Model Layer

Data Chunks

Say what?

Application

Application Data

HTTP, email, etc

Presentation

Session

Transport

Segments

TCP or UDP

Network

Packets/Datagrams

IP

Data Link

Frames

Ethernet

Physical

Bits/Bytes

Wired or Wireless

Let's read this table top-down. Your web browser can download web pages by sending an HTTP request to Google. This HTTP request passes to the transport layer, where a TCP header is added containing the source and destination port and other stuff. The result is passed to the network layer, where an IP header is added containing the source and destination port among other stuff. This is then broken up into pieces based on the Ethernet protocol, which works using hardware MAC addresses. Finally, the pieces are converted into electrical signals (representing bits and bytes) and sent out over the wire.

At the receiving end, this works in the opposite direction (bottom-up). The bits and bytes received over the wire are assembled into Ethernet frames. From these Ethernet frames, one or more IP packets are extracted. The IP header is removed, and the result is passed to the transport layer. The TCP header is removed, and from the information therein, the original HTTP request can be forwarded to the appropriate port on the receiving machine. It is then up to the application listening on that particular port (in this case a web server) to deal with the request appropriately (in this case by sending back an HTTP response).

Okay. So this was just a very brief summary of how the internet works... but you should at least realise the difference between the World Wide Web and the Internet. If you remember that the WWW is based on HTTP and port 80, you will realise that it is just one of countless services on the internet.

In tomorrow's article, we will see a practical example of how all this works.