Denizhalil

Server and Client Application in C

Introduction

The C programming language is a powerful language commonly used for developing computer programs. In this article, we will explore how to create a basic server and client application using the C programming language. Such an application can help you gain a fundamental understanding of data communication and network programming.

Server Application

First, let’s create the server application. The server will listen for incoming connections and handle data from clients. Here’s a basic template for a C server application:
You can view our server/client article we wrote with Python 😉

  1. Creating a Socket: First, it creates a socket to listen for incoming connections.
  2. Configuring the Server Address: It configures the server address, specifying the IP address and port number to bind to.
  3. Binding the Socket: The server binds the socket to the specified IP address and port number.
  4. Listening for Connections: It listens for incoming connections from clients.
  5. Accepting and Handling Connections: When a client connects, the server accepts the connection and processes it.
  6. Handling Communication: The server handles communication with the client, receiving data and, if necessary, sending a response.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 12345
#define MAX_CONNECTIONS 5
#define BUFFER_SIZE 1024

int main() {
    // Define socket and other variables
    int server_socket, client_socket;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_addr_len = sizeof(client_addr);

    // Create a socket
    server_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (server_socket == -1) {
        perror("Error: Failed to create socket");
        exit(EXIT_FAILURE);
    }

    // Configure the server address
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(PORT);
    server_addr.sin_addr.s_addr = INADDR_ANY;

    // Bind the socket
    if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
        perror("Error: Bind failed");
        close(server_socket);
        exit(EXIT_FAILURE);
    }

    // Listen for incoming connections
    if (listen(server_socket, MAX_CONNECTIONS) == -1) {
        perror("Error: Listen failed");
        close(server_socket);
        exit(EXIT_FAILURE);
    }

    printf("Listening on port %d...\n", PORT);

    // Accept incoming connections and process them
    while (1) {
        client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len);
        if (client_socket == -1) {
            perror("Error: Accept failed");
            continue;
        }

        printf("Connection accepted from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));

        // Handle communication with the client (you can define your own communication protocol here)
        char buffer[BUFFER_SIZE];
        ssize_t bytes_received;

        while ((bytes_received = recv(client_socket, buffer, BUFFER_SIZE, 0)) > 0) {
            buffer[bytes_received] = '\0';  // Null-terminate the received data
            printf("Received: %s", buffer);

            // Implement your server's response logic here
            // For example, you can send a reply to the client using the send() function.
        }

        close(client_socket);
        printf("Connection closed by the client.\n");
    }

    close(server_socket);
    return 0;
}

Client Application

Next, let’s create a client application that will communicate with the server:
For port scanning application with C

  1. Creating a Socket: It creates a socket for communication.
  2. Configuring the Server Address: The client configures the server’s IP address and port number.
  3. Connecting to the Server: It establishes a connection with the server.
  4. Sending Data: The client can send data to the server using the send() function.
  5. Receiving Data: It can also receive data from the server using the recv() function.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define SERVER_IP "127.0.0.1"  // Enter the server's IP address here
#define SERVER_PORT 12345
#define BUFFER_SIZE 1024

int main() {
    int client_socket;
    struct sockaddr_in server_addr;

    // Create a socket
    client_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (client_socket == -1) {
        perror("Error: Failed to create socket");
        exit(EXIT_FAILURE);
    }

    // Configure the server address
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SERVER_PORT);
    server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);

    // Connect to the server
    if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
        perror("Error: Failed to connect to the server");
        close(client_socket);
        exit(EXIT_FAILURE);
    }

    printf("Connected to the server at %s:%d\n", SERVER_IP, SERVER_PORT);

    // You can send data to the server using the send() function.
    char message[] = "Hello, server!";
    send(client_socket, message, strlen(message), 0);

    // You can also receive data from the server using the recv() function.
    char buffer[BUFFER_SIZE];
    ssize_t bytes_received = recv(client_socket, buffer, BUFFER_SIZE, 0);
    if (bytes_received > 0) {
        buffer[bytes_received] = '\0';  // Null-terminate the received data
        printf("Received from the server: %s\n", buffer);
    }

    // Close the socket
    close(client_socket);

    return 0;
}

And The Result

Conclusion

In this article, we’ve covered the basics of creating a server and client application using the C programming language. This application serves as a foundation for understanding network programming and allows you to define your own communication protocols and implement more complex features as needed.

Leave a Comment

Join our Mailing list!

Get all latest news, exclusive deals and academy updates.