Socket programming is a fundamental technology for network communication, enabling applications to communicate over a network. In this tutorial, we’ll explore how to create a basic socket program in C. By the end, you’ll have a foundational understanding of how to establish both client and server applications using sockets.
What is a Socket?
A socket is a software endpoint that establishes communication between two different processes on the same or different computers. In networking, sockets are used to send and receive data across a network.
Prerequisites
Before we begin, ensure you have a basic understanding of C programming language and have a C compiler installed on your system (e.g., GCC for Unix-like systems or MinGW for Windows).
Creating a Simple Socket Server
Step 1: Include Headers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
Step 2: Define Constants and Variables
#define PORT 8080
#define BUFFER_SIZE 1024
Step 3: Implement the Server Code
int main() {
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE] = {0};
char *hello = "Hello from server";
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
valread = read(new_socket, buffer, BUFFER_SIZE);
printf("%s\n", buffer);
send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");
return 0;
}
Step 4: Compile and Run the Server
Compile the server code using your C compiler (e.g., gcc server.c -o server
) and run the executable (./server
). The server will start listening on port 8080.
Creating a Simple Socket Client
Step 1: Implement the Client Code
int main() {
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
char buffer[BUFFER_SIZE] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <
;= 0) {
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed \n");
return -1;
}
send(sock, hello, strlen(hello), 0);
printf("Hello message sent\n");
valread = read(sock, buffer, BUFFER_SIZE);
printf("%s\n", buffer);
return 0;
}
Step 2: Compile and Run the Client
Compile the client code (gcc client.c -o client
) and run the executable (./client
). The client will connect to the server running on localhost
(127.0.0.1) at port 8080, send a message, and print the server’s response.
Conclusion
Socket programming in C provides a foundational understanding of how network communication works at a low level. From here, you can explore more advanced topics such as handling multiple clients, non-blocking I/O, and secure socket programming (SSL/TLS). Practice and experimentation are key to mastering socket programming, so feel free to modify and expand upon these examples to further your learning journey.
Note: Remember to compile and run these programs on a system where you have administrative privileges to bind to ports.