0% found this document useful (0 votes)
8 views6 pages

Parity

The document contains a TCP server and client implementation in C that communicates binary data with parity checking. The server listens for connections, receives 7-bit binary data along with a parity bit and type, checks the parity, and sends back a response indicating whether an error was detected. The client prompts the user for binary data and parity type, calculates the parity bit, and sends the complete message to the server.

Uploaded by

vitpythonboy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Parity

The document contains a TCP server and client implementation in C that communicates binary data with parity checking. The server listens for connections, receives 7-bit binary data along with a parity bit and type, checks the parity, and sends back a response indicating whether an error was detected. The client prompts the user for binary data and parity type, calculates the parity bit, and sends the complete message to the server.

Uploaded by

vitpythonboy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Server :

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#define BUFFER_SIZE 30

// Function to calculate parity based on type

char calculate_parity(char *data, char parity_type) {

int count = 0;

for (int i = 0; i < 7; i++) {

if (data[i] == '1')

count++;

// Even parity: ensure total number of 1s is even

if (parity_type == 'E')

return (count % 2 == 0) ? '0' : '1';

// Odd parity: ensure total number of 1s is odd

else

return (count % 2 == 0) ? '1' : '0';

int main() {

char buffer[BUFFER_SIZE];

int server_sock, client_sock;

struct sockaddr_in server_addr, client_addr;

socklen_t addr_size;
// Create socket

server_sock = socket(AF_INET, SOCK_STREAM, 0);

if (server_sock < 0) {

perror("[-] Socket error");

exit(1);

printf("[+] TCP server socket created.\n");

// Setup server address

server_addr.sin_family = AF_INET;

server_addr.sin_port = htons(5566);

server_addr.sin_addr.s_addr = INADDR_ANY;

// Bind socket

if (bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {

perror("[-] Bind error");

exit(1);

printf("[+] Bind successful.\n");

// Listen for incoming connections

listen(server_sock, 5);

printf("[+] Listening for connections...\n");

while (1) {

addr_size = sizeof(client_addr);

client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &addr_size);

printf("[+] Client connected.\n");

// Receive data from client

recv(client_sock, buffer, sizeof(buffer), 0);


buffer[9] = '\0'; // Null-terminate received data

printf("Received data (including parity bit and type): %s\n", buffer);

// Extract original 7-bit data, received parity bit, and parity type

char received_parity = buffer[7];

char parity_type = buffer[8]; // 'E' or 'O'

buffer[7] = '\0'; // Remove the parity bit

// Recalculate parity using the same type as the client

char calculated_parity = calculate_parity(buffer, parity_type);

// Check if parity matches

if (received_parity == calculated_parity)

strcpy(buffer, "No error detected.");

else

strcpy(buffer, "Error detected!");

// Send response to client

send(client_sock, buffer, strlen(buffer), 0);

printf("[+] Response sent to client.\n");

// Close connection

close(client_sock);

printf("[+] Client disconnected.\n\n");

return 0;

}
Client :
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#define BUFFER_SIZE 30

// Function to calculate the parity bit

char calculate_parity(char *data, char parity_type) {

int count = 0;

for (int i = 0; i < 7; i++) {

if (data[i] == '1')

count++;

// Even parity: ensure total number of 1s is even

if (parity_type == 'E')

return (count % 2 == 0) ? '0' : '1';

// Odd parity: ensure total number of 1s is odd

else

return (count % 2 == 0) ? '1' : '0';

int main() {

char *ip = "127.0.0.1";

int port = 5566;

int sock;

struct sockaddr_in addr;

char buffer[BUFFER_SIZE], message[50], parity_type;


// Create socket

sock = socket(AF_INET, SOCK_STREAM, 0);

if (sock < 0) {

perror("[-] Socket error");

exit(1);

printf("[+] TCP client socket created.\n");

// Setup address struct

addr.sin_family = AF_INET;

addr.sin_port = htons(port);

addr.sin_addr.s_addr = inet_addr(ip);

// Connect to server

if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {

perror("[-] Connection failed");

exit(1);

printf("[+] Connected to the server.\n");

// Get 7-bit data from user

printf("Enter 7-bit binary data: ");

scanf("%s", buffer);

// Choose even or odd parity

printf("Choose parity type (E for Even, O for Odd): ");

scanf(" %c", &parity_type);

// Calculate and append the parity bit using sprintf

sprintf(message, "%s%c%c", buffer, calculate_parity(buffer, parity_type), parity_type);


printf("Data sent (including parity bit and type): %s\n", message);

send(sock, message, strlen(message), 0);

// Receive server response

recv(sock, buffer, sizeof(buffer), 0);

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

// Close connection

close(sock);

printf("[+] Disconnected from the server.\n");

return 0;

You might also like