Socket programming is a way for two computers or programs to talk to each other over a network (like the internet or a local network).
There are two main types of sockets:
Stream Sockets (TCP)
- Connection-based, reliable communication.
- Guarantees data arrives in order without loss or duplication.
- Used for web browsing, email, and file transfers.
Datagram Sockets (UDP)
- Connectionless, unreliable communication.
- Faster but does not guarantee delivery or order.
- Used for video-streaming, online-gamine, and voice calls.
Note: The headers <netinet/in.h> and <sys/socket.h> are part of POSIX socket APIs and are supported on Unix/Linux-based systems. These libraries are not available on microcontroller platforms like Arduino (e.g., Arduino Uno). For Arduino, networking is typically done using hardware-specific libraries such as WiFi.h or Ethernet.h.
Server-Side Socket Programming Stages
1. Creating the Server Socket
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
- AF_INET : IPv4 protocol
- SOCK_STREAM: TCP socket
2. Defining Server Address
sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(8080);
serverAddress.sin_addr.s_addr = INADDR_ANY;
- htons(): Converts port to network byte order.
- INADDR_ANY: Accept connections on any IP.
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. Bind Socket to Address
bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress));
4. Listen for Incoming Connections
listen(serverSocket, 5);
5. Accept Client Connection
int clientSocket = accept(serverSocket, nullptr, nullptr);
6. Receive Data from Client
char buffer[1024] = {0};
recv(clientSocket, buffer, sizeof(buffer), 0);
cout << "Message from client: " << buffer << endl;
7. Close Server Socket
close(serverSocket);
Client-Side Socket Programming 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.
Then we sent the data using send() call. After all the operations are done, we close the connection using close() call.
1. Create Client Socket
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
2. Defining Server Address
sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(8080);
serverAddress.sin_addr.s_addr = INADDR_ANY;
3. Connect to Server
connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress));
4. Send Data to the Server
const char* message = "Hello, server!";
send(clientSocket, message, strlen(message), 0);
5. Close Client Socket
close(clientSocket);
Example of Socket Programming in C++
server.cpp
#include <cstring>
#include <iostream>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
using namespace std;
int main()
{
// creating socket
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
// specifying the address
sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(8080);
serverAddress.sin_addr.s_addr = INADDR_ANY;
// binding socket.
bind(serverSocket, (struct sockaddr*)&serverAddress,
sizeof(serverAddress));
// listening to the assigned socket
listen(serverSocket, 5);
// accepting connection request
int clientSocket
= accept(serverSocket, nullptr, nullptr);
// recieving data
char buffer[1024] = { 0 };
recv(clientSocket, buffer, sizeof(buffer), 0);
cout << "Message from client: " << buffer
<< endl;
// closing the socket.
close(serverSocket);
return 0;
}
client.cpp
#include <cstring>
#include <iostream>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
int main()
{
// creating socket
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
// specifying address
sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(8080);
serverAddress.sin_addr.s_addr = INADDR_ANY;
// sending connection request
connect(clientSocket, (struct sockaddr*)&serverAddress,
sizeof(serverAddress));
// sending data
const char* message = "Hello, server!";
send(clientSocket, message, strlen(message), 0);
// closing socket
close(clientSocket);
return 0;
}
By compiling and running server and client source files, we get the following output.
Output:

As we can see, the message we sent to the server at port 8080 is revived and printed by the server. We can also create a loop where we can keep sending the messages to the server.
Application of Socket Programming
- Web Servers and browsers
- Chat applications
- File transfer protocols (FTP)
- Online gaming platforms
- Distributed Systems
Advantages of Socket Programming
- Language Interoperability
- Efficient Communication
- Multi-user Handling