How to use sockets in c++ part 1

Today we are going to see, how we can use sockets in C++. As we all know, we are in the era of Computers where data is transferred every second as we speak. Every decent programmer should be well-versed in this topic. Socket programming is an essential topic in this area. In this article, we’ll look at socket programming and look at different ways to implement it in C++. 


Socket programming is a method of merging or connecting two nodes via a network so that they can communicate without losing any data. One socket (node) listens on a specific port at an IP address, while the other socket establishes a connection with it. While the client connects to the server, the server creates the listener socket.

What is Socket?

Sockets are a way for processes to communicate with one another. These procedures can take place on the same system or across multiple machines that are connected via a network. Data transfers in both directions after a socket connection is established until one of the endpoints ends the connection.

State Diagram

Sockets in c++ have the following state diagram

State diagram for sockets.
Procedure in Client-Server Communication
  • Socket: Create a new communication
  • Bind: Attach a local address to a socket
  • Listen: Announce willingness to accept connections
  • Accept: Block caller until a connection request arrives
  • Connect: Actively attempt to establish a connection
  • Send: Send some data over a connection
  • Receive: Receive some data over a connection
  • Close: Release the connection

Stages for server

Socket Creation

sockfd: A socket descriptor which is an integer. 

domain: It is an integer that specifies the communication domain. We use AF_ LOCAL for communication among processes running on the same host. For communicating between processes that are on different hosts connected by IPV4, we use AF_INET and if connected via IPV6 we can use AF_I NET 6.

Type: It represents communication type

  1. SOCK_STREAM: Transmission Control ProtocolTCP(reliable, connection oriented)
  2. SOCK_DGRAM: User Datagram Protocol (unreliable, connectionless)

protocol: protocol is simply a set of information that represents rules of data exchange. Protocol value for Internet Protocol(IP), which is 0. 

Command: int sockfd = socket(domain, type, protocol)


This aids in the manipulation of socket settings for the file descriptor sockfd. This is entirely optional, however it aids in address and port reuse. Errors such as “address already in use” are avoided.

Command: int setsockopt(int sockfd, int level, int optname,  const void *optval, socklen_t optlen);


The bind function links the socket to the address and port number supplied in addr after its creation (custom data structure). We bind the server to localhost in the sample code, thus we use INADDR_ANY to specify the IP address.

Command: int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);


It switches the server socket to a passive state, waiting for the client to approach the server and initiate a connection. The backlog specifies the maximum length to which the sockfd connection queue can expand. the client may receive an ECONNREFUSED error for a connection request when the queue is full.

Command: int listen(int sockfd, int backlog);


It takes the first connection request for the listening socket, sockfd, from the queue of pending connections, establishes a new connected socket, and returns a new file descriptor for that socket. The client and server have established a connection and are ready to communicate data at this point.

Command: int new_socket= accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

Stages for Client

Socket Connection

The connection process is exactly the same as that of the Server. Please go through Stages for the server for better understanding.


The connect() system function connects the socket indicated by sockfd to the address supplied by addr. In addr, we give the address and port of the server.

Command: int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);


So, this is a basic theory we need to know before moving on to the implementation part. In our next tutorial, we are going to implement a Client-Server program with sockets in C++ using the above concepts. Meanwhile please review these concepts and understand basic principles, you can read more about sockets here.

Scroll to Top