×

C++ Socket Programming

In this world, computer networking has become very important for sharing of data. Every good programmer has some knowledge about computer networking. Socket programming is one of the critical topics in computer networking. In this article, we will learn the topic of socket programming.

In C++, socket programming is the way of connecting two nodes over a network. With the help of socket programming, sharing of data between two nodes is possible without losing any data. One socket is connected to a particular port at an IP address, and the other socket is associated with the server.

What is a Socket?

In real life, the socket is used as a medium that is used to connect the two devices. It can be either a phone charger plugged into the socket or a USB cable into our laptop. In the same manner, the socket is used in the local network for communication purpose. When a socket is created simultaneously, its domain address has been created by the programs. The socket is used the exchanging the data between processes. These processes may be available on the same or different devices. With the help of a socket, the transfer of data is possible in both directions. There are two classes that are used in socket programming. These two classes are ClientSocket and ServerSocket.

Procedure in Client-Server Communication

  1. Socket- It is used to create a new communication.
  2. Bind- It is used to attach the local address to a socket.
  3. Listen- It is used to accept the connection.
  4. Accept- It is used to block the caller.
  5. Connect- It is used to establish the connection.
  6. Send- It is used to send the data over a connection.
  7. Received- It is used to receive the data over a connection.
  8. Close- It is used to release the connection.

What is a connection?

Connection is a type of relationship between two machines where two machines know about each other. They also know how to communicate with each other. A socket connection means two machines have information about each other, and the information is like network location (IP address) and TCP port. A socket is also similar to a program that is used for sending and receiving data, accepting the incoming connection and making outgoing connections. The server assigns the sockets. With the help of socket(), the server creates the socket. These sockets cannot be shared with another process.

Setsockopt: With the help of this server call, the file referred to the socket. It also helps us to reuse the address and port. It also prevents errors such as “address already in use”.

Bind: After the socket is created, then the bind call is used to connect the address and port number.

Listen: We used to listen to calls to make a program that allows the incoming connection. It is specified by socket arguments. It accepts the connection until the queue becomes completely empty. The backlog parameter is used to accept more than one connection at a time.

Accept: There are two types of connection-based sockets. These two sockets are SOCK_STREAM and SOCK_SEQPACKET. The accept() system call is worked with the combination of these two sockets. It is used to extract the connection which comes first from the queue. Then it creates a new connection for the socket. Then it returns a new file descriptor that refers to the socket. But the new socket is not in the listenable connection because The accept() affects the new socket. Then a new socket, i.e. argument sockfd, has been created with that second socket. After that, the dual-socket binds a connection and listen to all the socket.

Stages for Client

Socket connection: The socket connection for the client is the same as the socket connection for the server.

Connect: With the help of connecting (), we can initiate the connection for the socket. If the socket parameter “s” is a SOCK_DGRAM, then the data are sent permanently over the connection. If the socket parameter “s” is a SOCK_STREAM, then we can attempt the connection with another socket. So the name parameter decides the type of connection for the socket. The primary function of connect() call is to establish a connection with a foreign association. Here the parameter “s” is used for the unconnected datagram. For the socket type SOCK_STREAM, an active connection is initiated with the host. When the socket function calls successfully, the data are ready for sending over the connection.

Send/Receive: With the help of sending or receiving the call, we can able to communicate between two sockets. The address of stored data can be obtained by the addr_of_data, addr_of_buffer call. We can also calculate the buffer size by the len_of_data and len_of_buffer calls.

Steps to establish the connection in the socket:

The system call can establish a connection between a different client and server, but both are involved in the process of construction of the socket. A socket is used as the end of the interprocess communication between client and server. Both client and server have their own socket to establish the connection.

The steps involved in connecting a socket on the client side-

  1. We can create a socket with the help of a socket call.
  2. Then connect the socket to the server’s address with the help of a system call.
  3. Then use the read() and write() system call for sending and receiving the data over the connection.

The steps involved in connecting a socket on the server side-

  1. Create a socket with the help of a socket call.
  2. With the help of bind(), we attach the socket with an address. There is a port number for every server socket address.
  3. With the help of the listening () system call, we can listen for the connection.
  4. With the help of accept() system call, we can accept the connection. When the client connects with the server, then acceptance of the relationship starts.
  5. After that, sending and receiving data is possible over the connection.

Connecting Multiple Clients Without Multithreading

It is possible that multiple clients are also connected to a single server. This is possible by many methods. One of the methods among them is Multithreading. But the Multithreading process is complicated to code and debug, and also, the result is unpredictable. There is a chance of occurring of deadlock during the process. So we do not have to use the Multithreading process. Instead of Multithreading, we should use a select() system call.

What is the select() function?

The select () system call is a Linux command. It allows the server to monitor multiple file descriptors. After the data are sent by a file descriptor, it is activated. So we also called it an interrupt handler. Select() system call also provides the information of data that is read by the socket. It also gives information on the total number of sockets that are ready for connection. 

There are four types of macros that are associated with the select() system call.

  1. *FD_ZERO(set): It shows the total number of empty sets. All the sets should be clear before the connection.
  2. *FD_CLR(s, set): It removes all the sockets from an empty set.
  3. *FD_ISSET(s, set): it checks if the member returns back the actual value or not.
  4. *FD_SET(s, set): With the help of this call, we can add a socket in the sets,

With the help of these four macros and one select system call, we can handle multiple connections in a single server.


Related Topics

Differences between Local and Global Variable

Define Global Variable Global variables are those that may be accessible worldwide across a programme and are defined outside of any functions or blocks. It may be accessed by any function in...

3 minutes read.

What are local class and global class in C++

In C++, a class is a fundamental block that achieves object-oriented programming. The class holds its data members and member functions. Objects help to access the data elements and functions....

4 minutes read.

Passing a Vector to a function in C++

A pointer is sent to the function when we feed it an array. However, there are two ways to pass a vector: Pass by Value Pass by Reference A copy of a vector...

3 minutes read.

C++ Friend function

A friend function has the right to access all private and protected members of a class although it is defined outside that class' scope. Syntax class className{     ......     friend retyrn_type function_Name(argument);     .......   }   return_type function_Name(argument){     ......   } C++ friend function Example #include <iostream>   using namespace std;   class Length   {       private:           int meter;       public:           Length(): meter(5) { }           friend int addMethod(Length); //friend function declaration   };   int addMethod(Length l) // friend function definition   {       l.meter += 10; //accessing private data from non-member function       return l.meter;   }   int main()   {       Length L;       int totallength;       totallength=addMethod(L);       cout<<"Length: "<< totallength;       return 0;   } Output: Length: 15   C++ friend function...

1 minute read.

Vector in C++

Vector in C++ In today’s article, we will be learning all the things about vector in C++ and how vector is different form an array in C++. So basically, vector is very...

3 minutes read.

Stack in C++

Stack: The stack is a very popular data structure. It is the form of data structure that follows a particular order called FIFO(First-In-First-Out). In simple words, a stack is an Abstract...

4 minutes read.

Singleton Design Pattern in C++

Singleton is similar to the global variable. Singleton helps to have only one object of its kind and provides only single access. One of the key features of the singleton...

2 minutes read.

Single Handling in C++

Introduction: Single handling in C++ refers to a technique for processing multiple events or requests with a single function or handler rather than creating separate functions for each task. This allows...

5 minutes read.

Floating Point Operations and Associativity in C, C++ and Java

In this tutorial, we are going to compare Floating-point operations and the concept of associativity. Before we apply the concept of associativity in the floating-point operations in all three programming...

3 minutes read.

Include Guards in C++

In C++ programming, we frequently utilize a class more than once, so it is necessary to create a header file and include it in the main program. Now, occasionally a...

3 minutes read.

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.

Constructor Overloading

The program contains more than one constructor in a class with the same name, and different types of arguments are called constructor overloading. Calling of constructor depends on the number and types...

2 minutes read.

Lexicographically Next Permutation in C++

In this tutorial, we'll look at how to use C++ to generate the lexicographically next permutation of a string. The lexicographically next permutation is the larger permutation. "ACB," for example,...

3 minutes read.

C++ Interfaces

The C++ programming language provides programmers with a variety of capabilities and functions. It also enables object-oriented programming, which is essential while working on a project. It will be simple...

7 minutes read.

C++ Call by Reference

Call by Reference is a C++ method for passing arguments to a function that enables us to pass the actual memory address of the parameter rather than a copy of...

4 minutes read.

Programs to Print Pyramid Patterns in C++

We will explore how to use code to print a variety of patterns utilizing stars (*), numbers (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,.…)  and alphabets...

7 minutes read.

How to concatenate two strings in C++

In the C++ programming language, the concatenation of two or even more strings is covered in this section. The term "string concatenation" refers to a collection of characters that join two...

4 minutes read.

C++ Date and Time

The date and time formats in C++ will be covered in this article. Because C++ lacks a proper date and time format, we must rely on the c language. The...

7 minutes read.

Two dimension array

C++ Two dimension (2D) Array Two dimension (2D) array is an array of arrays. It is represented in the form of row and column. The elements of 2D array are accessed through the...

2 minutes read.

Skyline Problem in C++

We have given n rectangular buildings in a 2-dimensional city. Here, to compute the Skyline of the given n rectangle structures in a two-dimensional metropolis while removing hidden lines, the...

3 minutes read.