What is Socket in Computer Network? (Fully Explained)
A network socket is a software element within a computer network node that serves as an endpoint for both sending and receiving data. An application programming interface (API) for the networking framework outlines the structure and characteristics of a socket. Sockets are created only during the lifetime of a process in a node-based application.
Due to the standardization of TCP/IP protocols during the development of the Internet, the term "network socket" is predominantly associated with the Internet Protocol suite, and is therefore commonly referred to as an "Internet Socket". In this scenario, a socket's address, which consists of the transport protocol, IP address, and port number, is utilized to identify it externally to other hosts.
The software endpoint for internal inter-process communication (IPC) within a node, which often employs the same API as a network socket, is typically called a socket.
Pipes are created using the 'Pipe' system call, while sockets are established with the 'socket' system call. Across the network, the socket facilitates bidirectional FIFO communication. At both ends of the connection, a socket that connects to the network is established. Each socket possesses a distinct address, which is formed by an IP address combined with a port number.
In the majority of client-server applications, sockets are utilized. The server creates a socket, links it to a network port, and awaits a connection from the client. Once a socket is created, the client attempts to connect to the server's socket. Data transmission occurs once the connection is successfully established.
Types of Sockets
1. Datagram Socket
A datagram socket is a specific type of network socket where packets are sent and received without establishing a link. It functions similarly to a mailbox. Letters (data) are collected and delivered (transmitted) to a mailbox (receiving socket). It is classified as a connection-less socket.
2. Stream Socket
A stream socket refers to a specific type of network socket utilized within a computer's operating system. It facilitates a connection-oriented, sequenced, and distinct flow of data, free from record boundaries, while also providing clear methods for establishing and terminating connections, as well as for error detection. This can be likened to a telephone system, where a connection is established between two phones.
Network Socket Client Server Interaction
The diagram below illustrates the complete interaction between the Client and Server.
Function Call | Description |
---|---|
create() | This is used to create a socket |
bind() | This serves as a socket identifier, just like to a phone number used to reach someone |
connect() | Are you prepared to establish a connection? |
listen() | Ready to transmit a message |
accept() | This Confirmation is similar to accepting an incoming call from a sender. |
write() | This is used to transmit data |
read() | This is used to receive data |
close() | This is used to finalize a relationship |