1. Packet before socket
Network Interface Card(NIC) receives packet from the outside network. These packets are handed over to Network Stack of an operating system. The Network Stack refers to the software module that handles TCP/IP protocol. When an application sends data to TCP/IP stack, the headers should be added. On the contrary, when application receives the data, the stack processes the data by stripping off the headers.
The data recieved from the outside is stored in the memory. The kernel processes this data and hand it over to process via Socket. The operating system stores the data in the Socket Buffer. The socket buffer resides in the kernel memory. It is temporarily stored there before being sent to a process.
In summary, a packet is processed by the system. Then the data is sent to process via Socket.
2. Socket Interaction between OS and Application
The application reads the data from the Socket connected to the File Descriptor. If the process calls recv() or read() syscalls, the operating system reads the data from the Socket Buffer and send it to the process. Then the data is copied to the memory of process, so to be handled by the process.
Now we know how the socket is handled inside a single system. Let’s see how the socket is really used in connection.
3. Connection with Socket
Socket acts as an endpoint of a network communication. It sets communication channel between two systems. It consists of IP and Port. It is combined with the protocols: TCP and UDP.
3-a. TCP socket
TCP is a protocol used to send data reliably. The connection is made with 3-way handshake. When the connection is established, the sockets of each endpoints maintain established states and close the connection only when the data transmission is done.
Unlike UDP socket, the ip and the port of source and destination are both used. It is because to maintain session.
3-b. UDP socket
UDP sockets use the connectionless protocol UDP, which means that there is no need to establish a connection before sending data. Unlike TCP, UDP does not maintain a connection state between the client and server. Instead, it transmits independent datagrams. Each datagram contains the destination IP address and port number.
UDP sockets are used in situations where fast transmission is necessary, or where maintaining a connection state is not required, such as in streaming or gaming.
4. Conclusion
To sum it up, data transmission over a network involves several key steps, starting with the Network Interface Card (NIC) receiving packets and handing them off to the Network Stack in the operating system. This stack processes the data and passes it to the relevant process via a socket, which acts as the endpoint for communication. TCP and UDP, the main transport protocols, handle connections differently—TCP offers reliable, connection-oriented communication, while UDP focuses on faster, connectionless transmission. Understanding how sockets fit into this process is essential for managing network communication effectively, whether it’s for web services, gaming, or streaming.
Leave a Reply