Socket Programming in C (Client–Server Communication)
Objective
To implement a TCP socket program in C that enables communication between a client and
a server over a network.
Theory
Socket programming allows processes on different systems to communicate using the
network. There are two main types of sockets:
- Stream Sockets (SOCK_STREAM) – Use TCP protocol (reliable, connection-oriented).
- Datagram Sockets (SOCK_DGRAM) – Use UDP protocol (unreliable, connectionless).
In this experiment, we use TCP (Transmission Control Protocol) sockets for reliable
communication.
Algorithm
Server Side
1. Create a socket using socket().
2. Bind the socket to an IP address and port using bind().
3. Listen for incoming client connections using listen().
4. Accept a connection using accept().
5. Receive message from the client using recv().
6. Send response to client using send().
7. Close the connection using close().
Client Side
1. Create a socket using socket().
2. Connect to the server using connect().
3. Send message to the server using send().
4. Receive response from the server using recv().
5. Close the socket using close().
Server Program (TCP Server in C)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
char buffer[BUFFER_SIZE] = {0};
char *message = "Hello from Server!";
int addrlen = sizeof(address);
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("Bind failed");
close(server_fd);
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("Listen failed");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("Server is listening on port %d...\n", PORT);
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) <
0) {
perror("Accept failed");
exit(EXIT_FAILURE);
}
printf("Client connected.\n");
recv(new_socket, buffer, BUFFER_SIZE, 0);
printf("Client: %s\n", buffer);
send(new_socket, message, strlen(message), 0);
printf("Message sent to client: %s\n", message);
close(new_socket);
close(server_fd);
return 0;
}
Client Program (TCP Client in C)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main() {
int sock = 0;
struct sockaddr_in serv_addr;
char buffer[BUFFER_SIZE] = {0};
char message[BUFFER_SIZE];
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("Invalid address or address not supported.\n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("Connection failed");
return -1;
}
printf("Enter message to send: ");
fgets(message, BUFFER_SIZE, stdin);
send(sock, message, strlen(message), 0);
recv(sock, buffer, BUFFER_SIZE, 0);
printf("Server: %s\n", buffer);
close(sock);
return 0;
}
How to Run (Linux Terminal)
gcc server.c -o server
gcc client.c -o client
./server (Run in one terminal)
./client (Run in another terminal)
Expected Output
Server Terminal:
Server is listening on port 8080...
Client connected.
Client: Hello Server
Message sent to client: Hello from Server!
Client Terminal:
Enter message to send: Hello Server
Server: Hello from Server!