Socket programming is a way of establishing communication link between two processes on two nodes (or machines) on a network to communicate with each other. These processes can reside on same machine or different. Socket programming uses several socket APIs (Application programming interface) to implement this communication channel. Some of the common functions involved in socket programming are socket(), bind(), listen() etc.

Advertisement

What is socket

Sockets are used by application processes to interact with other application processes. Sockets act as an interface between the application process and the transport layer and serve as an abstract that helps the application process to send and receive messages. Sockets create an end to end connection between two communicating processes within the same machine or on two different machines. In unix terminology, socket is also called as a file descriptor which is basically an integer associated with an open file.

Why socket programming is needed

The Internet has become the global media for communication and has changed the way we undertake our daily activities. These daily activities include from E-commerce to communicating with each other. But ever wonder how the computers on the Internet connection with each other so that we can carry out these activities. The answer is with the help of a network programming model called Sockets.

Many emerging businesses now want to use the Internet to provide their services. Using the Internet to provide services requires the software engineers or developers to have the skill of developing Internet-enabled applications or port the legacy applications on to the Internet platform. The essential elements needed in developing Internet-enabled applications is the sound knowledge of network programming model like Sockets and to make them work, we need socket programming.

Types of Sockets

Stream Sockets

Data sent through stream sockets arrive at the receiver in the same order. These sockets use TCP (Transmission Control Protocol) for data transmission. The sender receives an error message on an unsuccessful transmission of the data.

Datagram Sockets

They use UDP (User Datagram Protocol). They are connectionless because you don’t need to have an open connection as in Stream Sockets. Here the packet is built with destination information and then it is sent out.

Raw Sockets

A general user does not use raw sockets. They are used by those who are want to develop new communication protocols.

Sequenced Packet Sockets

Sequenced packet sockets allow the users to change the Internet Datagram Protocol (IDP) headers on a packet or a group of packets. They help them do that by allowing the user to either write a prototype header or a default header. It also allows the user to receive headers on the incoming packets.

Client-Server communication using Sockets

All network-based communications consist of a client and server.  A Client is the one that requests for a service from the server. A Server is the one that processes that request and provides the requested service to the client.

When a client requests service from a server or when a server sends the result to the client they communicate with the help of networking services offered by the transport layer. Transport layer consists of two protocols TCP (Transport Control Protocol) and UDP (User Datagram Protocol). Both these protocols make use of a programming interface called Sockets.

A server has a socket assigned to a port on which it listens for client requests. The server establishes a connection with the client by creating a new socket on a different port so that the server can continue to listen for client requests on the original socket while still serving the connected client.

Important functions used in Socket programming

Some of the important functions used in socket programming are:

Socket()

It creates a new communication endpoint.

Bind()

It attaches a local address to a socket.

Listen()

It tells that the socket is ready to accept connections

Accept()

Blocks a caller until a connection request arrives.

Connect()

Actively attempts to establish a connection.

Send()

Send some data through the established connection.

Receive()

Receive some data through the established connection.

Close()

Terminate the connection.

A typical Client program for Socket programming

1) Creating a Socket

To create a socket, we use the function socket(). The syntax of the function is int socket(int domain, int type, int protocol). This function returns a descriptor used to handle the socket.

Domain parameter in the function indicates the protocol family. It should have a value PF_INET for the Internet. Type indicates the type of socket it is. It can be a stream socket or a datagram socket. For stream socket, the value will be SOCK_STREAM, and for a datagram socket, the value will be SOCK_DGRAM. The protocol indicates which protocol it is whether TCP or UDP.

2) Connecting to a server

Establishing a connection to the server is done with the help of function connect(). The syntax of the function is int connect(int sockfd, struct sockaddr *server_address, socklen_t addrlen). The function would return 0 if the connection were successfully established otherwise it returns -1.

Sockfd is socket file descriptor. Sockaddr is the structure that stores the IP address and the port number.

3) Sending and Receiving data

The function used to send data is send(). The syntax of the function is ssize_t send(int sockfd, void *buf, size_t line). This function returns the number of characters sent if the data was sent successfully otherwise it returns -1.

The functions used to receive data is recv(). The syntax of the function is ssize_t recv(int sockfd, void *buf, size_t line). This function returns the number of characters received if the data was received successfully otherwise it returns -1.

4) Closing the socket

The function used to close the open sockets is close(). The syntax of the function is int close(int sockfd).

A typical server program for Socket programming

1) Creating the socket

To create a socket, we use the function socket(). The syntax of the function is int socket(int domain, int type, int protocol). This function returns a descriptor used to handle the socket.

Domain parameter in the function indicates the protocol family. It should have a value PF_INET for the Internet. Type indicates the type of socket it is, whether it is a stream socket or datagram socket. For stream socket, the value will be SOCK_STREAM, and for a datagram socket, the value will be SOCK_DGRAM. The protocol indicates which protocol it is whether TCP or UDP.

2) Preparing the created socket

We first need to associate the socket with a local address and port number on the server with the help of bind() function. The syntax of bind() function is int bind(int sockfd, struct sockaddr *myaddr, socklen_t addrlen). This function returns 0 on the successful binding of the socket; otherwise, it returns -1.

Next, we have to make the socket listen for incoming connections using listen() function. The syntax of listen function is int listen(int sockfd, int backlog). This function returns 0 on success and -1 on failure.

3) Accepting a new connection

Whether to approve or deny a new connection request from a client is decided using accept() function. The syntax of this function is int accept(int sockfd, struct sockaddr *addr, socklen_t addrlen). This function returns the socket descriptor of the new connection established.

If the socket has not received any connection request yet, then the accept function does a blocking operation, waiting for the connection request. If the socket gets too many connection requests, then some are rejected.

Client and server operations together.

The server creates a new socket with socket() function then binds that socket using bind() with its local address and port number. Then the server listens on this socket for client requests using listen(). The server then decides whether to accept or deny client request using accept().

Advertisement

The client, on the other hand, creates a socket on its machine using socket(). Then the client sends the connection request to the server using connect().

The server and client can exchange data using send(), and recv() function.

Final Remarks

Understanding the network programming model like sockets helps us to move our applications from offline to the Internet. Socket programming on the outset comes off as complex but due to vast socket APIs and libraries available a software developer should find it easy to incorporate them in their networking applications.

Socket programming is a way of establishing a communication link between two processes on a network to communicate with each other. Some of the common functions involved in socket programming are socket(), bind(), listen() etc.
Advertisement