Sockets
Sockets are file descriptors that serve as the communication end-points for processes running on a operating system like Linux. A socket connection is a bidirectional communication interface that allows two processes to exchange information within a network.
Sockets are the interface to use the TCP protocol. Sockets allow applciations to send and receive data from a TCP connection just like reading and writing to a file using a file descriptor. Hence they are used to implement client and server applications. The server process creates a socket and listens on it for clients' requests.
The main two types of sockets in networking:
SOCK_STREAM
: Stream sockets ensure that data is delivered in the order it was sent and without errors. For example web browsing (HTTP), email (STMP), etc use this socket type (TCP).SOCK_DGRAM
: Datagram sockets send packets of data, called datagrams, without establishing a connection or ensuring delivery. For example video streaming, online gaming etc. use this socket type (UDP).
Each network socket is associated with an IP address and a port number, identifying both the host and a specific application or service.
Flow of events
Sockets used in a client-server model has a typical flow of events. The following figure shows the typical flow of events (and the sequence of issued APIs) for a connection-oriented socket session. An explanation of each event follows the figure.
We will be using the following functions to setup TCP connections:
int socket() Create an endpoint for communication
Argument Name | Argument Type | Description |
---|---|---|
domain | int | Specifies the communication domain (e.g., AF_INET for IPv4). |
type | int | Specifies the communication semantics (e.g., SOCK_STREAM for TCP). |
protocol | int | Specifies a particular protocol to be used with the socket. (default 0) |
int bind() Binds a socket to a local address
Argument Name | Argument Type | Description |
---|---|---|
sockfd | int | File descriptor of the socket to bind. |
addr | const struct sockaddr * | Pointer to a sockaddr structure that contains the address to bind to. |
addrlen | int / socklen_t | Size of the address structure. |
int listen() Listen for connections on a socket
Argument Name | Argument Type | Description |
---|---|---|
sockfd | int | File descriptor of the socket(server) to listen on. |
backlog | int | maximum length of the queue maintaining pending connections |
int connect() Initiates a connection on a socket.
Argument Name | Argument Type | Description |
---|---|---|
sockfd | int | File descriptor of the socket(client). |
addr | const struct sockaddr * | Pointer to a sockaddr structure that contains the address to connect to (server address). |
addrlen | int / socklen_t | Size of the address structure. |
int accept() Accepts an incoming connection on a listening socket.
Argument Name | Argument Type | Description |
---|---|---|
sockfd | int | File descriptor of the listening socket. |
addr | const struct sockaddr * | Pointer to a sockaddr structure to receive the address of the connecting entity. |
addrlen | int / socklen_t | Size of the address structure. |
int send() Send a message on a socket
Argument Name | Argument Type | Description |
---|---|---|
sockfd | int | File descriptor of the connected socket. |
buf | const void * | Pointer to he buffer containing the data to be sent. |
len | int | Length of the data to be sent. |
flags | int | Bitwise OR of flags controlling the operation(Default 0). |
int recv() Recieves a message from a socket
Argument Name | Argument Type | Description |
---|---|---|
sockfd | int | File descriptor of the connected socket. |
buf | const void * | Pointer to the buffer to receive the data. |
len | int | Length of the buffer. |
flags | int | Bitwise OR of flags controlling the operation(Default 0). |
int close() Closes a socket
Argument Name | Argument Type | Description |
---|---|---|
sockfd | int | File descriptor of the socket to close. |
- Socket creation: The process begins with the creation of a socket using the
socket()
system call. This call initializes a communication endpoint and returns a file descriptor. - Binding (optional): In server applications, the socket may be bound to a specific address and port using the
bind()
system call. This step is necessary for servers to listen for incoming connections on a specific network interface and port. (Bind is optional for client sockets as the operating system assigns a local address and port automatically) - Listening (Server Only): Servers then enter a listening state using the
listen()
system call, indicating their readiness to accept incoming connections from clients. - Connection Establishment (Client): Clients initiate a connection to the server by using the
connect()
system call, specifying the server's address and port. This call establishes a connection to the server, allowing for data exchange. - Accepting Connections (Server): Upon receiving a connection request from a client, the server accepts the connection using the
accept()
system call. This call creates a new socket specifically for communication with the client. - Data Exchange: Once the connection is established, both the client and server can send and receive data using the
send()
andrecv()
system calls, respectively. Data sent by one party is received by the other, allowing for bidirectional communication. - Connection Termination: When communication is complete, either party can initiate the termination of the connection using the
close()
system call. This releases the allocated resources associated with the socket and terminates the communication channel.