Javatpoint Logo
Javatpoint Logo

Socket Programming in C/C++

In today's world, computer networks play an important role in the data transfer field. It is a subject that each programmer should know of. Under the computer network, socket programming is one of the most important topics in the programming world. In this topic, we are doing to discuss socket programming and the different method of socket programming that is implemented in C++.

In C++, socket programming is a method that combines two or more nodes with each other over a network so that the nodes can share the data without any loss of the data. In this connection, one node listens to one port which is connected to a particular IP address. When the client reaches the server, the server creates the socket listener.

What is a Socket?

Let's understand about the socket by talking the real-time example. A socket is a type of medium that provides a connection between two devices. The socket can be either a phone charger that provides the connection between the socket and the phone or the phone and that laptop. With the help of a socket, different applications are attached to the local network with different ports. Every time when the socket is created, the server specifies the program, and that program specifies the socket and the domain address.

The socket is a type of mechanism that is used to exchange data between different processes. Here these processes are either present in different devices or the same device which are connected over a network. Once the connection for the socket is created, then the data can be sent in both directions and continues until one of the endpoints closes the connection.

Socket Programming in C/C++

Procedure in Client-Server Communication

There are some procedures that we have to follow to establish client-server communication. These are as follows.

  1. Socket: With the help of a socket, we can create a new communication.
  2. Bind: With the help of this we can, we can attach the local address with the socket.
  3. Listen: With this help; we can accept the connection.
  4. Accept: With this help; we can block the incoming connection until the request arrives.
  5. Connect: With this help; we can attempt to establish the connection.
  6. Send: With the help of this; we can send the data over the network.
  7. Receive: With this help; we can receive the data over the network.
  8. Close: With the help of this, we can release the connection from the network.

Stages for Server Socket Creation

There are some stages by which we can create the socket for the server. These are as follows.

  1. int socketcr: Socket(domain, type, protocol)
  2. Socketcr: It is an integer type, and it is like a file handler.
  3. Domain: It is a communication domain and it is an integer type.
  4. Type:It is a communication type.
  5. SOCK_DGRAM: It is a type of UDP which is unreliable and connectionless.
  6. Protocol: It is used to assign the protocol value for the IP address, which is 0. The protocol value is similar to the value appearing in the protocol field of the IP header of the pocket.

What is a Connection?

A connection is a type of relationship between two machines where the two software are known about each other. These two software know how to establish a connection with each other; in other words, we can say that this two software know how to send the bits over the network. A connection of the socket means the two machines should know all the information between each other, like the phone number, IP address, and the TCP port.

A socket is a type of object which is similar to the file that allows the program to accept the incoming connection and allow them to send or receive the incoming connection. Also, it is a type of resource assigned to the server's process.

The server can create the socket with the help of the socket(). This socket can not be shared with any other processor.

  • Setsockopt: With the help of Setsockopt, we can manipulate the various option of the socket, which are referred to by the socket's file descriptor. This process is completely optional. With the help of Setsockopt, we can reuse the port and address of the client and the server. When the server gives the error " address already in use," we can prevent it with the help of Setsockopt.
  • Bind: We can bind the socket with the address and the port with the help of the bind function. This operation is done after the creation of the socket. For example, if we try to bind the server with the local host, then we use INADDR_ANY to define the server's IP address.
  • Listen: We can make a connection mode socket with the help of the listening to () function. An example of a connection mode socket is SOCK_STREAM. This can be defined by the socket argument. This is used to accept the incoming connection perform the queue operation for the incoming connection, and perform the backlog of the incoming connection. When an incoming connection requests the server for acknowledgment, the socket is put into passive mode. The backlog parameter of the server refers to the fact that it cannot allow more than one connection at a time to the server. If some incoming connection has come and the queue is full, then the server provides the error with an indication of " ECONNREFUSED." With the help of listen(), the incoming connection is on hold, and when the queue is empty, it calls all the incoming connections to the server.
  • Accept: With the help of accept() system call; we can make the connection-based socket. Some connection-based sockets are SOCK_STREAMand SOCK_SEQPACKET. It extracts all the incoming connections that come in first and allows their request to go to the server. The newly connected list is not able to listen with the help of another argument for the creation of the new socket.

Stages for Client

  1. Socket connection: It is exactly the same as the method for the creation of the server.
  2. Connect: We can initiate a connection to the socket with the help of connect() system call. If the parameter for the socket is a type of SOCK_DGRAM, then we can define the datagram as permanent with the help of connect(). If the socket is of type SOCK_STREAM, then we can attempt to make another connection for the server. With the help of connect() function, we can also create a connection for the foreign association. If the socket is unbound, then the system assigns the unique value to the local association. When the system successfully calls completed, the socket is ready to send or receive any type of data.
  3. Send/Receive: The send() and recv() functions can perform the below operation.
  • The socket on which the data can be communicated with each other.
  • The storage buffer can store data about the address, like addr_of_data and addr_of_buffer.
  • It deals with the size of the buffer, like len_of_data and len_of_buffer.
  • It deals with the flag that says how the data will be sent.

Steps to Establish the Connection in the Socket

It establishes a connection between the different clients and the server. But both the client and the server can handle the socket connection. Each process has to establish a connection for its own socket.

The steps involved in establishing a socket on the client side are as follows:

  • It creates a socket with the help of a socket() system call.
  • Then we have to connect with the socket address of the server with the help of a system() call.
  • Then we have to send and receive the data. We can do this in various ways. we can do this read() and write() function.

The steps involved in establishing a socket on the server side are as follows:

  • It first creates a socket with the help of a socket() system call.
  • Then it binds the socket to an address with the help of the bind() system call. An address consists of a port number for the server socket in the host machine.
  • Then it listens for the connection with the help of the listening () system call.
  • Then the server accepts the incoming connection with the help of accept() system call. It also blocks all incoming commands until a client is connected to a server.
  • Then the process of sending and receiving data starts.

Connecting Multiple Clients without Multithreading

There are various examples in which we see how a single user can connect with the server. In today's programming world, multiple users are connected to the server with different sockets.

There is various ways to achieve this. One of them is multithreading. With the help of multithreading, we can achieve this. We can implement a multithreading process with the help of by the help of the select() function.


Code for the client:


Socket Programming in C/C++


Socket Programming in C/C++

Uses of Socket Programming

Socket programs are used to communicate between various processes, usually running on different systems. It is mostly used to create a client-server environment. This post provides the various functions used to create the server and client program and an example program.

In the example, the client program sends a file name to the server, and the server sends the contents of the file back to the client. Socket programming usually pertains to basic communication protocols like TCP/UDP and raw sockets like ICMP. These protocols have a small communication overhead when compared to underlying protocols such as HTTP/DHCP/SMTP etc.

Some of the basic data communications between the client and server are:

  • File Transfer: Sends name and gets a file.
  • Web Page: Sends URL and gets a page.
  • Echo: Sends a message and gets it back.


  • C++ can establish communication only with the machine requested and not with any other machine on the network.
  • Sockets allow only raw data to be sent. This means that the client and server need mechanisms to interpret the data.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA