Socket Programming

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
6.7 kB
0
Indexable
Never
//client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 8080
#define BUFFER_SIZE 1024

// Function to calculate CRC
int calculateCRC(const char *data) {
    int crc = 0;

    for (int i = 0; i < strlen(data); ++i) {
        crc ^= (int)data[i] << 8;
        for (int j = 0; j < 8; ++j) {
            if (crc & 0x8000) {
                crc = (crc << 1) ^ 0x1021; // Polynomial: x^16 + x^12 + x^5 + 1
            } else {
                crc = crc << 1;
            }
        }
    }

    return crc & 0xFFFF;
}

// Function to calculate LRC
int calculateLRC(const char *data) {
    int lrc = 0;
    for (int i = 0; i < strlen(data); i++) {
        lrc ^= data[i];
    }
    return lrc;
}

// Function to calculate VRC (using even parity)
int calculateVRC(const char *data) {
    int count = 0;
    for (int i = 0; i < strlen(data); i++) {
        if (data[i] == '1') {
            count++;
        }
    }
    return (count % 2 == 0) ? 0 : 1;
}

// Function to calculate simple checksum
int calculateChecksum(const char *data) {
    int checksum = 0;
    for (int i = 0; i < strlen(data); i++) {
        checksum += data[i];
    }
    return checksum;
}

int main() {
    struct sockaddr_in serv_addr;
    int sock = 0;
    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;
    }

    char data[BUFFER_SIZE];
    printf("Enter data to send: ");
    fgets(data, BUFFER_SIZE, stdin);

    // Ask user for error detection method
    printf("Choose an error detection method:\n");
    printf("1. CRC\n");
    printf("2. LRC\n");
    printf("3. VRC\n");
    printf("4. Simple Checksum\n");
    printf("Enter your choice: ");

    int choice;
    scanf("%d", &choice);
    while(getchar() != '\n'); // Clear input buffer

    int errorBits = 0;
    switch (choice) {
        case 1: // CRC
            errorBits = calculateCRC(data);
            break;
        case 2: // LRC
            errorBits = calculateLRC(data);
            break;
        case 3: // VRC
            errorBits = calculateVRC(data);
            break;
        case 4: // Simple Checksum
            errorBits = calculateChecksum(data);
            break;
        default:
            printf("Invalid choice\n");
            return -1;
    }

    // Send data with error detection bits to server
    send(sock, data, strlen(data), 0);
    send(sock, &errorBits, sizeof(int), 0);
    printf("Data sent successfully\n");

    close(sock);
    return 0;
}


//server.c


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

#define PORT 8080
#define BUFFER_SIZE 1024

// Function to calculate CRC
int calculateCRC(const char *data) {
    int crc = 0;

    for (int i = 0; i < strlen(data); ++i) {
        crc ^= (int)data[i] << 8;
        for (int j = 0; j < 8; ++j) {
            if (crc & 0x8000) {
                crc = (crc << 1) ^ 0x1021; // Polynomial: x^16 + x^12 + x^5 + 1
            } else {
                crc = crc << 1;
            }
        }
    }

    return crc & 0xFFFF;
}

// Function to calculate LRC
int calculateLRC(const char *data) {
    int lrc = 0;
    for (int i = 0; i < strlen(data); i++) {
        lrc ^= data[i];
    }
    return lrc;
}

// Function to calculate VRC (using even parity)
int calculateVRC(const char *data) {
    int count = 0;
    for (int i = 0; i < strlen(data); i++) {
        if (data[i] == '1') {
            count++;
        }
    }
    return (count % 2 == 0) ? 0 : 1;
}

// Function to calculate simple checksum
int calculateChecksum(const char *data) {
    int checksum = 0;
    for (int i = 0; i < strlen(data); i++) {
        checksum += data[i];
    }
    return checksum;
}

int main() {
    int server_fd, new_socket, valread;
    struct sockaddr_in address;
    int opt = 1;
    int addrlen = sizeof(address);
    char buffer[BUFFER_SIZE] = {0};
    int errorBits = 0;

    // 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);
    }

    // Read data and error bits from client
    valread = read(new_socket, buffer, BUFFER_SIZE);
    if (valread <= 0) {
        printf("Error reading data from client\n");
        exit(EXIT_FAILURE);
    }
    read(new_socket, &errorBits, sizeof(int));

    // Verify data based on error detection method
    int errorDetected = 0;
    switch (errorBits) {
        case 1: // CRC
            if (calculateCRC(buffer) != 0) {
                errorDetected = 1;
            }
            break;
        case 2: // LRC
            if (calculateLRC(buffer) != 0) {
                errorDetected = 1;
            }
            break;
        case 3: // VRC
            if (calculateVRC(buffer) != 0) {
                errorDetected = 1;
            }
            break;
        case 4: // Simple Checksum
            if (calculateChecksum(buffer) != 0) {
                errorDetected = 1;
            }
            break;
        default:
            printf("Invalid error detection method\n");
            break;
    }

    // Output result
    if (errorDetected) {
        printf("Error detected in received data!\n");
    } else {
        printf("Data received without error: %s\n", buffer);
    }

    close(new_socket);
    close(server_fd);
    return 0;
}
Leave a Comment