Networking introduction

First I will give you an introduction to basic networking principles and terms. Anyone with internet access will have some knowledge about networks, servers, clients, but to ensure you know enough to program with it I've included this chapter. You won't need all the details mentioned here when programming winsock, but it's good to know something about the underlying techniques.

1. Networks and protocols

You probably already know what a network is, it's a collection of computers connected to each other so they can exchange data. There are several types of networks, such as LANs (Local Area Network), WANs (Wide Area Network) and of course the internet. To ensure that all traffic is going smoothly, networks rely on protocols:

Protocol
A protocol is a set of rules describing the format in which data is transmitted over a network.

As stated in the information box above, a protocol describes how to communicate over a network. It can be compared with a human language: at the lowest level nearly everyone can make and hear sounds (compare: electronic signals) but people won't understand each other unless they speak a according to a specific language they both understand (compare: protocol).

2. Ethernet

Networks rely on several protocol layers, each one having its own task in the communication process. A very commonly used configuration is the ethernet LAN with TCP/IP. In ethernet LANs, computers can be connected using coaxial, twisted pair (UTP) or optic fiber cables. Nowadays, for most networks, UTP cables are used. WANs and the internet (partly a combination of many WANs) use many of the techniques used in ethernet LANs, so I will discuss ethernet LAN technology first.

MAC

The lowest layer of ethernet is the hardware level, called the Media Access Layer, or MAC for short. This layer can be a network card, for example, which contains the serial network interface and controller that take care of converting the raw data into electronic signals and sending it to the right place.

Package that are sent over a network of course need to reach their destination. So there has to be some kind of addressing. Various levels of the ethernet interface have different addressing methods, as you will see later. At the lowest MAC level, addressing is done with MAC numbers.

MAC number
48-bit identifier that is hardcoded into each network interface unit. The allocation of these numbers is done by the IEEE Registration Authority so each ethernet chip has a world wide unique number (that is, if the manufacturer didn't mess up :). MAC numbers are often noted as colon-separated hex numbers: 14:74:A0:17:95:D7.

To send a packet to another network interface, the packet needs to include its MAC number. LANs use a very simple method to send the packets to the right interface: broadcasting. This means that your network card just shouts the package to every other interface it can reach. Each receiving interface looks at the destination MAC number of the packet, and only buffers it if matches its own MAC number. While this method is easy to implement and quite effective on LANs, bigger networks (WANs, internet) don't use this method for obvious reasons; you wouldn't want everyone on the internet to send packets to everyone else on the internet. WANs use better routing mechanisms, which I won't discuss here. Just remember that at the lowest level, addressing is done with MAC numbers. Ethernet packets also include a CRC and error detection.

IP

Just above the hardware level is the IP level. IP simply stands for Internet Protocol. Just like the MAC layer, IP too has its own way of addressing:

IP number
The numbers used to address at the IP level of the network interface. IPv4, the version most widely used uses 32-bit values, noted in the well known dotted format: 209.217.52.4. Unlike MAC numbers, IP numbers are not hardcoded into the hardware, they are assigned to it at software level.

IP numbers shouldn't be something strange to you. The internet uses them to uniquely identify a specific computer. IP addresses can be assigned to a network interface using software. Doing this associates the IP number with the MAC address of the network interface. To address using IP numbers, the associated MAC number needs to be resolved. This is done with the ARP (Address Resolution Protocol). Each host maintains a list with pairs of IP and MAC numbers. If an IP is used without a matching MAC number, the host sends out a query packet to the rest of the LAN. If any of the other computers in the LAN recognize their IP number, it sends back the corresponding MAC number. If no matching MAC number can be found the packet is sent to the gateway, a computer that forwards packages to external networks. The IP to MAC conversion is actually done at the data link layer (MAC layer)

The IP protocol adds the source and destination address (IP numbers) to the packet, as well as some other package properties such as the TTL hops (time to live hops), the protocol version used, header checksum, sequence count and some more fields. They are not important to us so I won't explain them in detail.

TCP

The next layer is the TCP layer (or alternatively, the UDP layer). This layer is very close to the network application and deals with many things. As final addition to the addressing, TCP adds a port number to the package:

Port number
While IP numbers are used to address a specific computer or network device, port numbers are used to identify which process running on that device should receive the package. Port numbers are 16-bit, and thus limited to 65536 numbers. A process can register to receive packets sent to a specific port number ('listening'). A notation often used when addressing a port number on a device is 'IP:portnumber', eg. 209.217.52.4:80. Both sides of a connection use a port number, but not necessarily the same.

Many port numbers are WKP (Well Known Ports), that is they are commonly associated with a specific service. For example, the WWW uses port 80 by default, FTP uses port 21, e-mail uses 25 (SMTP) and 110 (POP). Although these are the ports usually used for those services, nobody prevents you from using different ports. However, it's a good practice to use port numbers higher than 1024 for other, custom services.

While the IP layer doesn't care about the success of transmissions, TCP does. The TCP layer ensures data does arrive, and correctly. It also lets the receiver control the data flow, ie. the receiver can decide when to receive data. If a package is lost during the way to its destination, TCP resends the package. TCP also reorders the packages if they arrive in an order different from the original order. This makes the programmer's life easy as it can safely assume the data that is sent is received and in the right order. UDP, an alternative for TCP, does not have these features and cannot guarantee the arrival of packages. TCP is connection-oriented, and the best choice for continuous data streams. UDP on the other hand is connectionless, and packet oriented. I won't deal with UDP in this tutorial.

Software

Finally, above the TCP layer is the network software. In windows, your application does not directly access the TCP layer but uses the WinSock API. The software layer provides a very convenient way of dealing with networking. Thanks to all the underlying layers, you don't need to worry about packets, packet size, data corruption, resending of lost packets etc.

3. The ethernet interface stack

Encapsulation with different protocol layers

The image above shows the encapsulation of the each protocol in the ethernet interface stack. It all starts with the software layer, which has a piece of data that it wants to send over the network. Even this data usually has a format (eg. HTTP, FTP protocols), although not shown in the image. The user data first gets a TCP header including the source and destination port number. Then the IP header is added, containing the source and destination IP address. Finally the data link layer adds the ethernet header, which specifies the MAC numbers of the source and destination. This is the data that is actually sent over the wires. As you can see there's a lot of overhead in an TCP/IP package. The overhead can be minimized by choosing a large enough data size for the package. Luckily winsock will arrange this for you.

4. Links