What are sockets?
Sockets allow communication between two processes in the same or different machines across a network and defined protocols. A socket is one endpoint of the communication and provides full-duplex
Sockets have many applications. They are mainly used in client-server applications where the server creates a socket and assigns a port number, and then keeps the socket alive throughout the communication process to handle the client's requests. On the other end, the client also creates a socket at their end and connects to the server socket. Once a connection is established, the data can be transferred between the client and server.
Types of sockets
The three main types of sockets in networks are as follows:
Datagram socket: It is a connection-less service to transfer data packets over the network. Datagram (packets) are routed independently, irrespective of their original packet collection. It uses the
protocol for data transfer which means it performs better than stream packets. However, in this communication, reliability is not guaranteed. Packets can be received out of order and, in the worst case, can be missed.UDP User datagram protocol Stream socket: It is a connection-oriented service where data is sent as a stream of bytes. It uses flow control to avoid data overrun and
protocol for data transfer, making it more secure and reliable (data arrives in order and without duplication). In case packets are received with an error, TCP requests only the corrupted packets to be sent again. Some of the function calls used in stream sockets are:TCP Transmission control protocol Create(),Bind(),Listen(),Connect(),Accept(),Write(),Read()andClose().Raw socket: As the name suggests, it provides communication for the raw packets, bypassing the TCP protocols. This socket is used in the lower level of the network architecture that offers direct access to the underlying provider/ protocol, such as ICMP (Internet control message protocol). Raw sockets can be used to test new protocols implementation over the architecture as well.
Which socket to use
Choosing the type of socket is determined based on the user's priority for data integrity. If data integrity is the prime concern, then the stream socket is suitable, and if performance is required and not integrity, then datagram sockets need to be used.
Free Resources