Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

SOCKET

A socket is a software interface that enables two computers to communicate with one
another across a network, such as the Internet. It is an endpoint for data transmission
and reception between programs on various devices. The internet connection provides
the facility to the one process from computer C1 can communicate to a process from a
computer C2.

It consists of following features.

Reliable: A dependable method of data transport via an internet connection is provided


by sockets. They provide accurate and error-free data transmission while establishing a
secure communication path between the client and server.

Point to Point: Sockets connect two particular endpoints in a point-to-point fashion.


Data can be transferred and received at the desired location since its IP address and port
number can recognize each endpoint.

Full Duplex: Communication that is full-duplex is one of sockets' primary characteristics.


This implies that data can be transferred concurrently between the client and server in
both ways. Data can be sent and received by both the client and the server, enabling
interactive and bidirectional communication.

How Socket Works?

Sockets enable communication between clients and servers using several protocols,
including the User Datagram Protocol (UDP) and the Transmission Control Protocol
(TCP).

Socket can use connection-oriented TCP protocol, data packets will always be sent in the
proper order. It creates a link between the client and server, guaranteeing the accuracy
and dependability of data transfer.

UDP is a lightweight, connectionless protocol that puts speed above dependability. Low
latency is crucial for real-time applications, where it is often employed.

Client Socket

o To connect to a server, client programs employ client sockets.


o They start a conversation by making queries to the server. Then they get replies.
o Client connections are temporary and made for certain interactions or activities.
o Ephemeral ports, which the operating system dynamically assigns, are often used
by them for communication.
o Data sent to the server and received must be processed by client sockets.
Server Socket:

o Applications running on servers employ server sockets to check for new client
connections.
o They wait for connections from clients and manage several client connections
simultaneously.
o Server sockets have a long lifespan and are intended to be always available.
o They operate by listening on specified ports linked to particular services or
protocols.
o Server sockets are in charge of receiving incoming client connections and
responding with services or data.
o They can frequently manage several client connections at once by setting up
different threads or processes for every connection.

Python Socket Module:

The Python socket module provides the methods required for generating and using
sockets. Socket.socket() is the primary technique for creating sockets. The syntax for the
socket() function is as follows:

1. Create_Socket = socket.socket(socket_family, socket_type, protocol=0)

The socket() method's arguments are as follows:

o socket_family: The address family for the socket is specified by socket_family. It


can be either AF_UNIX (for sockets in the Unix domain) or AF_INET (in the Internet
domain).
o socket_type: the socket's type is specified by socket_type. For TCP sockets, it can
be SOCK_STREAM, and for UDP sockets, it can be SOCK_DGRAM.
o protocol: specifies the particular protocol to be used. It is normally set to 0,
enabling the operating system to select the proper protocol following the provided
socket_type.

In most cases, we will work with AF_INET sockets (for the Internet domain) and
SOCK_STREAM sockets (for TCP). Once the socket is created, we can use various methods
provided by the socket module to interact with the socket, such as bind(), connect(),
listen(), send(), recv(), and so on.

These methods allow us to bind the socket to a specific address and port, establish a
connection to a remote server, listen for incoming connections, send data, receive data,
and perform other socket-related operations.
In the client-server socket model, the client socket connects to the server while the server
socket waits for incoming connections. While the client socket utilizes the connect()
method to create a connection, the server socket sets up the socket using the bind() and
listen() methods

Client Sockets Methods

The client socket method is given below.

connect()

This function is used to set up a connect to a remote socket at an address. An address


format contains the host and port pair which is used for AF_INET address family.

Server Socket Methods

The server socket methods are given below.

bind()

This method is used to bind the socket to an address. The address's format depends on
socket family mentioned above (AF_INET).

listen(backlog)

This method is used to listen for the connection made to the socket. The backlog is a term
which used to represent the maximum number of the queued connection that must be
listened before rejecting the connection.

accepts()

The accepts() method accepts a connection. The socket should be bound to an address
and ready to listen the connections. It returns the pair(conn, address) where con is a
new socket object which can be used to send and receive data on the connection,
and address is the address linked to the socket on the other end of the connections.

Few Common Socket Methods

A few commonly used functions for server object is given below.

1. Server_Object = socket.socket(socket_family, socket_type, protocol = 0)


TCP Socket Methods UDP Socket Methods

Server_Object.recv() - Receives TCP Server_Object.recvfrom() - Receive UDP


messages messages.

Server_Object.send() - Transmit TCP Server_Object.sendto() - Transmits UDP


messages messages.

Types of Sockets

In networking, there are two different kinds of sockets: SOCK_STREAM and


SOCK_DGRAM. Let's examine each kind in more detail:

SOCK_STREAM:

o TCP (Transmission Control Protocol)-based communication uses SOCK_STREAM


sockets.
o They offer a dependable stream of data that is connection-focused.
o Data is transferred via SOCK_STREAM in a continuous stream, guaranteeing that
it will arrive in the same order as it was sent.
o These sockets ensure data delivery and automatically manage any
retransmissions or missed packets.
o Applications that demand dependable and organized data delivery, such as web
surfing, file transfers, email, and real-time communication protocols like HTTP,
FTP, and SSH, frequently employ SOCK_STREAM sockets.
o Data may be delivered and received in both directions since the communication is
bidirectional.

SOCK_DGRAM:

o For UDP (User Datagram Protocol)-based communication, SOCK_DGRAM sockets


are utilized.
o They provide an unstable, connectionless datagram service.
o Data is transmitted via SOCK_DGRAM in separate packets known as datagrams,
each of which is a distinct communication unit.
o Datagrams may be lost or duplicated during transmission, and there is no
assurance that they will be received in the order they were sent.
o Applications like real-time multimedia streaming, online gaming, DNS, and SNMP
that can tolerate the odd packet loss or out-of-order delivery frequently employ
SOCK_DGRAM sockets.
o Data may only be sent in one direction during unidirectional communication.

You might also like