×

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

fread() Function in C++ Programming

C++ language is used to make high-performance applications that can work efficiently, and it is one of the world's most popular languages. It is an object-oriented and high-level programming language;...

3 minutes read.

The Stock Span Problem

It is necessary to determine the span of a stock's price throughout all n days in order to solve the stock span problem, which involves a set of n daily...

5 minutes read.

C++ Program to find largest subarray with 0 sum

Write a program to find the largest subarray that has a sum zero. The array contains positive and negative numbers. Print the length of the max subarray whose sum turns...

4 minutes read.

C++ operator

In this article, we will discuss about the operators in C++ with their types and examples. An operator is specially a symbol that tells compiler to perform specific manipulation. C++ contains...

5 minutes read.

C++ Maximum Index Problem

Given an array A[] of positive integers. We will find the maximum of (j-i) such that i and j are the indexes of A[] and A[i] <= A[j], i<=j For...

5 minutes read.

C++ Program For FCFS (First Come First Serve)

The most basic scheduling technique is FCFS, often known as "FIFO (First In, First Out)". In this procedure, the first one is utilized and executed first, while the second one...

4 minutes read.

Type conversion in C++

Type conversion is the process of changing the type of variables in a program. The ultimate goal of type conversions is to allow variables of a single data type operate with...

7 minutes read.

C++ int into String

Data type conversion is a standard editing process. You may need to convert variable from one type of data to another in a variety of situations. There are two ways...

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.

Web Development in C++

Before learning above C++ web development, we need to learn about CGI What is CGI? CGI stands for common gateway interface. CGI is a standard that tells us how the exchange of...

4 minutes read.

C++ Tricks for Competitive Programming

If you are interested in Computer science or Information technology, you must have heard about competitive programming. Competitive programming is a way to improve your problem-solving skills. There are various...

7 minutes 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.

Type difference of Character literals in C VS C++

Character literals in C: In C, a character literal is represented by a single character enclosed in single quotes, such as 'a' or 'b'. It is of type int. This means...

5 minutes read.

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++ 1/2 decimal equal is 0.555555555555555555555 .... An indefinite number of lengths will require the storage...

4 minutes read.

Hospital Management Project in C++

The following capabilities are required to build a hospital management project: These are as follows: hospitals' names, contact information, and lists of doctors and patients. Activities Supported Hospital Data Print Patients' data to...

4 minutes read.

C++ Continue in for loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.

Compile Time Polymorphism in C++

What is Polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. One application of polymorphism in...

4 minutes read.

strcat() vs strncat() in C++

In this tutorial, we will explore about strcat() and strncat() in the most usable language C++. We will also look at the difference between them. strcat() C++ is a computer language with...

4 minutes read.

C++ Program to find the element that occurs once

Write a program to find the element in the array that occurs once. Given that all the numbers in the array are present two times and the array is sorted,...

7 minutes read.

Multiset in C++

Introduction Multisets are part of the C++ STL, or Standard Template Library. In C++, a multiset is a set of associative containers that hold ordered items. Items in a multiset can...

10 minutes read.