An Introduction to Computer Networks

Introduction

Welcome to this blog series, where I will be discussing about the basics of Computer Networks and Network Programming in C. For the programming part, you have to be little bit comfortable with pointers and structs as it is one of the most used in network programming. In this blog, however I'll be talking about what is a socket and types of socket. For the network programming part, I'll be using gcc and will be compiling it in a Linux system.

What is a Network Socket?

A network socket is nothing but a endpoint for communication between two devices in a network, that is used to recieve and send data over a network. It is a interface to share data with each other.

The server creates a socket, binds it to an IP header and a port number and makes it search for connections. Then, the client creates another socket and connects itselves to the server. The data is sent by the socket in the server to the socket in the client that is waiting to receieve the data. When the communication is done the, the connection in closed.

This is the basic idea behind a socket and how it is created and managed. Now, Let us see what are the different types of network sockets that can be used to transmit data.

Types of Network Sockets:

Stream Socket

Stream Socket is a two-way reliable connected communication streams. If we give two items in an order into the socket say, "a, b" then in the output it arrives "a, b" and they will also be error-free.

Applications like telnet and ssh use stream socket. Web browsers also use stream sockets to get the web pages. Stream sockets are used in these situations because for the application to work perfectly fine we require that all the packets reach and all the packets reach in the order as we specify.

Stream sockets have hihg level of data transmission quality because they use, "TCP" (Transmission Control Protocol). The job of TCP is that it ensures that all the packets reach the destination sequentially and error-free. You must have heard TCP in the term TCP/IP. In TCP/IP, "IP" stands for Internet Protocol, they are used in internet routing meaning that they ensure that the destination and the sender has an address so that the packet can reach to the desired address destination.

Datagram Socket

Datagram Socket is one another important network socket. Datagram sockets also use IP for internet routing but they don't use TCP instead they use "UDP" (User Datagram Protocol). Datagram sockets are also called "connectionless sockets" and they are not as reliable as Stream sockets. They are considered not reliable because when using a datagram socket, the packet may arrive, may arrive out of order and its error-free.

They are called connectionless sockets because they don't need to establish a open connection like stream sockets to send data. Just build a packet, slap on a IP header and then send it. They are generally used in cases when there is no availability of TCP or in cases when losing packets do not matter.

TFTP (Trivial File Transfer Protocol) and DHCPCD (DHCP Client) and multiplier games, audio and video calls use Datagram Sockets. In the case of TFTP and DHCPCD, to avoid loss of data, they use a protocol on top of UDP, which ensures whether a packet as reached the destiantion or not. Whenever a packet is sent using TFTP, the protocol expects a packet in return from destination saying that it has recieved the packet and are called ACK packets or Acknowledgement packets. If the ACK packets are not recieved then the socket keeps sending the same packet until the ACK packet is recieved.

Datagram sockets are used in audio, video calls and multiplier games as they do not require all the data in ordered format.

One of the main reasons to using datagram sockets is that they are very fast than stream sockets as there is no need to ensure whether all the packets are reached and whether they have reached in an order.

Raw Sockets

Raw sockets are a type of network socket that allows direct access to lower-level network protocols. Unlike standard sockets (like TCP or UDP sockets), raw sockets enable users to construct their own packet headers and interact directly with network protocols such as ICMP, IP, or custom protocols.

Key Features of Raw Sockets:

Common Use Cases:

Raw sockets let you bypass all the fancy, safe, hand-holding of TCP and UDP and just craft your own packets. You want to forge an ICMP packet? Go ahead. You want to send a custom TCP SYN packet without a full TCP handshake? Be my guest. You want to build your own networking protocol entirely from scratch? Raw sockets are the way. With great power comes great responsibility, though. Because raw sockets give you the bare-metal ability to send and receive packets, you can do all kinds of weird, cool, dangerous, and amazing things. Network monitoring, custom protocol development, security testing… or if you're feeling mischievous, packet spoofing (not that I’m saying you should do that).

Raw socket is one of the best sockets, its pretty much like Linux, we can configure everything we want from scratch or build on top or in whatever way we want to. Raw Sockets are mostly run as sudo in Linux or "Run as administrator" in Windows. Some features are also restricted by some operating system, restrict? an OS? yes you guessed it right, its "Windows" that restricts Raw sockets from sending TCP packets. Since, we have to define all the headers by ourselves unlike TCP and UDP where the OS builds the headers, but for raw sockets we have to do everything manually.

Summary

In summary, Network Socket is nothing but a endpoint in devices that recieves and send packets to the destination. The server builds a socket, the socket listens to connections, recieves the packet and sends it to the desitnation and the connection and socket are closed. There are three main types of sockets: Stream, Datagram and Raw Sockets. Stream socket is a reliable two way socket and ensures that all the packets reach destination and in an ordered form. Datagram packets are very fast and send data without a open connection (connectionless) and raw sockets are low-level network sockets where we can define our own headers for packets, and interact directly with protocols like ICMP and IP and raw sockets are highly secure way of sending packets and require admin perms to run them and takes a lot of time to get them configured. In the next blog, it'll be a short one on the Networking model, the classic one and the OSI model.

Thanks for reading the blog