What Is Linux, Unix Socket?
Socket is a mechanism which provides connection between two process or system by using network stacks. In a more clear way we can use
sockets communicate and transfer data between two system. As we know Unix and Linux systems works on file logic. Everything in operating system is 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 Socket library with the following code. Python also support encryption of socket communication.