EvolveDev
Theme toggle is loading

Beginner's Guide to Socket Programming in C: Step-by-Step Explanation

Learn how to implement socket programming in C by building both server and client application. We'll cover every concept step by step, with code exampples.

Published: Nov 26, 2024

Beginner's Guide to Socket Programming in C: Step-by-Step Explanation

Introduction

We’ll learn how to implement socket programming in C by building both server and client application. This is a beginner friendly tutorial that anybody can follow.

What You’ll Learn 🌟

What is Socket Programming?

Socket programming enables communication between two applications over a network. In this tutorial, we’ll create:

Setting Up the Server

Here is the server.c program explained step by step:

#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8080
int server_fd, new_socket;
ssize_t valread;
struct sockaddr_in address;
int opt = 1;
socklen_t addrlen = sizeof(address);
char buffer[1024] = { 0 };
char* hello = "Hello from server";

Creating the Socket

if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket failed");
    exit(EXIT_FAILURE);
}

Configuring the Socket

if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
    perror("setsockopt");
    exit(EXIT_FAILURE);
}

Binding the Socket

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");
    exit(EXIT_FAILURE);
}

Listening for Connections

if (listen(server_fd, 3) < 0) {
    perror("listen");
    exit(EXIT_FAILURE);
}

Accepting a Client Connection

if ((new_socket = accept(server_fd, (struct sockaddr*)&address, &addrlen)) < 0) {
    perror("accept");
    exit(EXIT_FAILURE);
}

Handling Communication

valread = read(new_socket, buffer, 1024 - 1); // Read client message
printf("%s\n", buffer); // Print the client's message
 
send(new_socket, hello, strlen(hello), 0); // Send a response
printf("Hello message sent\n");

Setting Up the Client

Here’s the client.c program explained step by step:

#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8080

Creating a Socket

if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("\n Socket creation error \n");
    return -1;
}

Setting Up the Server Address

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("\nInvalid address/ Address not supported \n");
    return -1;
}

Connecting to the Server

if ((status = connect(client_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) < 0) {
    printf("\nConnection Failed \n");
    return -1;
}

Sending and Receiving Data

send(client_fd, hello, strlen(hello), 0); // Send message to server
printf("Hello message sent\n");
 
valread = read(client_fd, buffer, 1024 - 1); // Read server's response
printf("%s\n", buffer);

Running the Programs

  1. Compile:
    gcc server.c -o server
    gcc client.c -o client
  2. Run the Server:
    ./server
  3. Run the Client:
    ./client

Output

Refer to my GitHub Repository for the πŸ‘‰ Source Code

#c#programming

Share on:

Recommended

Copyright Β© EvolveDev. 2025 All Rights Reserved