What Is Socket In Linux? – POFTUT

What Is Socket In Linux?


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.

Use Cases

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

Socket Types

There are four main socket types. Stream sockets and Datagram sockets are popular types.

Stream Socket

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.

Stream Socket
Stream Socket

Datagram Socket

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.

Raw Socket

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 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.

Java Socket
Java Socket

Python Socket

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.

import socket

LEARN MORE  How To Scan All TCP and UDP Ports with Nmap?

Leave a Comment