Skip to main content

Client Server Program for Primality Checking

Below is an example of a simple client-server program in C. The client sends a number to the server, and the server determines whether the number is prime or not and sends the result back to the client

Server Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdbool.h>

#define PORT 8080

bool is_prime(int num) {
    if (num <= 1) return false;
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0) return false;
    }
    return true;
}

int main() {
    int server_fd, new_socket;
    struct sockaddr_in address;
    int addrlen = sizeof(address);
    char buffer[1024] = {0};
    char response[1024];

    // Create socket file descriptor
    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
        perror("Socket failed");
        exit(EXIT_FAILURE);
    }

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(PORT);

    // Bind the socket to the network address and port
    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
        perror("Bind failed");
        close(server_fd);
        exit(EXIT_FAILURE);
    }

    // Listen for incoming connections
    if (listen(server_fd, 3) < 0) {
        perror("Listen failed");
        close(server_fd);
        exit(EXIT_FAILURE);
    }

    printf("Server is listening on port %d...\n", PORT);

    // Accept a connection
    if ((new_socket = accept(server_fd, (struct sockaddr *)&address, 
                             (socklen_t *)&addrlen)) < 0) {
        perror("Accept failed");
        close(server_fd);
        exit(EXIT_FAILURE);
    }

    // Read data from the client
    read(new_socket, buffer, 1024);
    int number = atoi(buffer);

    // Determine if the number is prime
    if (is_prime(number)) {
        snprintf(response, sizeof(response), "%d is a prime number.", number);
    } else {
        snprintf(response, sizeof(response), "%d is not a prime number.", number);
    }

    // Send the response back to the client
    send(new_socket, response, strlen(response), 0);

    printf("Processed request for number: %d\n", number);

    // Close the sockets
    close(new_socket);
    close(server_fd);

    return 0;
}

Client Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 8080

int main() {
    int sock = 0;
    struct sockaddr_in serv_addr;
    char buffer[1024] = {0};
    char input[100];

    // Create socket file descriptor
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        printf("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("Invalid address/ Address not supported\n");
        return -1;
    }

    // Connect to the server
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        printf("Connection failed\n");
        return -1;
    }

    printf("Enter a number: ");
    scanf("%s", input);

    // Send the number to the server
    send(sock, input, strlen(input), 0);

    // Read the response from the server
    read(sock, buffer, 1024);

    printf("Server: %s\n", buffer);

    // Close the socket
    close(sock);

    return 0;
}

Instructions

  1. Compile the server and client programs:

    gcc -o server server.c gcc -o client client.c
  2. Start the server:

    ./server
  3. In another terminal, run the client:

    ./client
  4. Enter a number in the client terminal. The server will check if the number is prime and send the result back to the client.

Example Output

Server

Server is listening on port 8080... Processed request for number: 17

Client

Enter a number: 17 Server: 17 is a prime number.

Comments

Popular posts from this blog

CSL 332 Networking Lab KTU 2019 Scheme - Dr Binu V P

CSL 332 Networking Lab KTU BTech 2019 Scheme About Me Scheme Syllabus Experiments 1.Learn the Networking Commands and Network Configuration Files     Basic networking commands     More Networking commands     Network configuration Files     View the configuration and address of your network interface     Network Connectivity      View Active TCP connections     MAC address of another machine using ARP  2.  System calls in Network Programming 3.  Simple TCP/IP Client Server Program 4.  Simple UDP Client Server Program 5.Application Programs     Concurrent UDP Time Server     Checking Prime Numbers 6. Simulate ARQ Protocols  / sliding window protocols          Stop and Wait           Go-Back-N          Selective Repeat  7. Routing Protocols - Distance Vector and Link State   ...

Stop and Wait ARQ

Here's a simple C program that demonstrates the Stop-and-Wait ARQ protocol. This basic implementation simulates the sender transmitting packets one at a time and waiting for an acknowledgment from the receiver. If the acknowledgment is not received, the sender retransmits the packet. Key Points: The sender sends one packet at a time. If the receiver acknowledges it (ACK), the sender sends the next packet. If the acknowledgment is lost, the sender retransmits after a timeout. C Program: Stop-and-Wait ARQ Simulation #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h>  // for sleep() #define TIMEOUT 3  // Timeout duration in seconds #define TOTAL_PACKETS 5  // Number of packets to send int simulate_acknowledgment() {     // Simulate a 70% chance of successful acknowledgment     return rand() % 10 < 7; } int main() {     srand(time(0));  // Seed for random number generation     i...

Go-Back-N ARQ

Here's a simple Go-Back-N ARQ implementation using C socket programming to simulate communication between a client (sender) and a server (receiver). Overview: Go-Back-N ARQ allows the sender to send multiple packets (window size) without waiting for individual ACKs. If a packet is lost or an ACK is not received, all packets starting from the lost packet are retransmitted. The server randomly simulates ACK loss or successful reception. Program Structure: Server : Simulates ACK reception with a chance of ACK loss, acknowledging packets up to the first lost packet. Client : Sends packets in a sliding window fashion, retransmitting the entire window if an ACK is lost. Server - Receiver #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <time.h> #define PORT 8080 #define BUFFER_SIZE 1024 #define LOSS_PROBABILITY 30  // 30% chance of ACK loss int main() {     int server_fd, new_soc...