Professional Documents
Culture Documents
Socket
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.
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 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.
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:
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
connect()
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.
Types of Sockets
SOCK_STREAM:
SOCK_DGRAM: