Server Stages

1. Creating the Server Socket

We create socket by using the socket() system call. It is defined inside the <sys/socket.h> header file.

Syntax

int serverSocket = socket(AF_INET, SOCK_STREAM, 0);

here,

  •  socketfd: It is the file descriptor for the socket.
  • AF_INET: It specifies the IPv4 protocol family.
  • SOCK_STREAM: It defines that the TCP type socket.

2. Defining Server Address

We then define the server address using the following set of statements

sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(8080);
serverAddress.sin_addr.s_addr = INADDR_ANY;

here,

  • sockaddr_in: It is the data type that is used to store the address of the socket.
  • htons(): This function is used to convert the unsigned int from machine byte order to network byte order.
  • INADDR_ANY: It is used when we don’t want to bind our socket to any particular IP and instead make it listen to all the available IPs.

3. Binding the Server Socket

Then we bind the socket using the bind() call as shown.

bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress));

4. Listening for Connections

We then tell the application to listen to the socket refffered by the serverSocket.

listen(serverSocket, 5);

5. Accepting a Client Connection

The accept() call is used to accept the connection request that is recieved on the socket the application was listening to.

int clientSocket = accept(serverSocket, nullptr, nullptr);

6. Receiving Data from the Client

Then we start receiving the data from the client. We can specify the required buffer size so that it has enough space to receive the data sent the the client. The example of this is shown below.

char buffer[1024] = {0};
recv(clientSocket, buffer, sizeof(buffer), 0);
cout << "Message from client: " << buffer << endl;

7. Closing the Server Socket

We close the socket using the close() call and the associated socket descriptor.

close(serverSocket);

Socket Programming in C++

In C++, socket programming refers to the method of communication between two sockets on the network using a C++ program. We use the socket API to create a connection between the two programs running on the network, one of which receives the data by listening to the particular address port, and the other sends the data. One of the features of the socket programming is that it allows the bidirectional communication between the nodes.

In this article, we will create some simple C++ programs to demonstrate the use of socket programming.

Similar Reads

What are Sockets?

Sockets can be viewed as the endpoint of the two-way communication between the two programming in the network. They are generally hosted on different application ports and allow bidirectional data transfer....

Server Stages

1. Creating the Server Socket...

Client Stages

Similar to server, we also have to create a socket and specify the address. But instead of accepting request, we send the connection request when we can to sent the data using connect() call....

Example of Socket Programming in C++

server.cpp...

Application and Benefits

...

Contact Us