The socket is a mechanism that provides a connection between the two-processor system by using network stacks. In a more clear way, we can use sockets to communicate and transfer data between two systems. As we know Unix and Linux systems work on file logic. Everything in the operating system is a file where network connections are files too.
Sockets can be used in different cases.
- We can use socket to transfer data between two different process in same system.
- Send command to the remote system
- Download data from remote system
There are four main socket types. Stream sockets and Datagram sockets are popular types.
Stream sockets uses TCP protocol for transmission. So this makes stream socket reliable way for transmission. If there is a network related problem we will get error messages about it. If we send X, Y, Z data the remote will get them in the same order X, Y, Z.
Datagram sockets uses UDP which makes the data delivery inconsistent. Another feature of datagram socket is connection-less. The sender just send data and can not be sure whether it is arrived to the destination.
As its name suggest Raw sockets do not provide ready to use protocol. It is mainly used for creating new protocol where both side can understand. Raw sockets do not used regularly.
Sequenced Packet Socket
Sequenced Packet Socket is similar to the Stream Socket but have record boundaries. Sequnced Packet Socket is not popular way to use socket.
Java programming language provides socket support.As we can see we need to use IP address and Port number in order to create a socket. Socket related function can be found in
java.net.Socket name space The communication diagram is like below.
Python programming language also provides Socket related libraries. We can import the Socket library with the following code. Python also supports encryption of socket communication.