Set up a multi-client TCP server. The client will take the user’s data word and divisor and send them to the server. Using CRC, the server will determine the codeword and send it to the client. Both clients and server must display the codeword.
Client Program
//This code is written by Souvik Ghosh and Debmitra Chatterjee
//Client program
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX_SIZE 50
int main()
{
int sock_desc;
struct sockaddr_in serv_addr;
char sbuff[MAX_SIZE],rbuff[MAX_SIZE];
char data[MAX_SIZE],gen_poly[MAX_SIZE];
char CRC[MAX_SIZE], rcvData[MAX_SIZE];
if((sock_desc = socket(AF_INET, SOCK_STREAM, 0)) < 0)
printf("Failed creating socket\n");
bzero((char *) &serv_addr, sizeof (serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
serv_addr.sin_port = htons(3000);
if (connect(sock_desc, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) {
printf("Failed to connect to server\n");
return -1;
}
printf("Connected successfully!!!!\n");
printf("Enter data to be transmitted: ");
scanf("%s",data);
printf("Enter divisor: ");
// get the generator polynomial
scanf("%s",gen_poly);
//send the data size
int dataSize= strlen(data);
int divSize = strlen(gen_poly);
int rcvCRCSize,rcvDataSize;
//send the size of data
write(sock_desc,&dataSize,sizeof(int));
write(sock_desc,&divSize,sizeof(int));
//send the actual data
write(sock_desc,&data,dataSize*sizeof(int));
write(sock_desc,&gen_poly,dataSize*sizeof(int));
printf("Data Sent!!!!!\n");
printf("waiting for the server...\n");
//Receive data
//receive the size of data
read(sock_desc,&rcvCRCSize,sizeof(int));
read(sock_desc,&rcvDataSize,sizeof(int));
//receive the actual data
read(sock_desc,CRC,rcvCRCSize*sizeof(char));
read(sock_desc,rcvData,rcvDataSize*sizeof(char));
printf("Data Received!!!!!\n");
//print data
printf("CRC is: %s\n",CRC);
printf("Final data: %s\n",rcvData);
close(sock_desc);
return 0;
}
Server Program
//This code is written by Souvik Ghosh and Debmitra Chatterjee
//Server Program
#include<stdio.h>
#include<string.h> //strlen
#include<stdlib.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
#include<pthread.h> //for threading , link with lpthread
#define MAX_SIZE 50
//the thread function
void *connection_handler(void *);
int count;
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , *new_sock;
struct sockaddr_in server , client;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 3000 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
c=sizeof(struct sockaddr_in);
while(client_sock=accept(socket_desc,(struct sockaddr*)&client,(socklen_t*)&c))
{
count++;
printf("Connection %d accepted\n",count);
pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = client_sock;
if( pthread_create( &sniffer_thread , NULL , connection_handler , (void*) new_sock) < 0)
{
perror("could not create thread");
return 1;
}
puts("Handler assigned");
}
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
return 0;
}
/*
This will handle connection for each client
*/
void *connection_handler(void *socket_desc)
{
//Get the socket descriptor
int sock = *(int*)socket_desc;
int n;
char rcvData[MAX_SIZE], rcvDiv[MAX_SIZE], CRC[MAX_SIZE];
int rcvDataSize,rcvDivSize;
int i,j;
//read the size of data
recv(sock,&rcvDataSize,sizeof(int),0);
recv(sock,&rcvDivSize,sizeof(int),0);
//read the actual data
recv(sock,rcvData,rcvDataSize*sizeof(int),0);
recv(sock,rcvDiv,rcvDivSize*sizeof(int),0);
/**************************************************CRC*************************************************/
// initializing check_value
for(i=0;i<rcvDivSize;i++){
CRC[i]=rcvData[i];
}
do{
// check if the first bit is 1 and calls XOR function
if(CRC[0]=='1'){
for(j = 1;j < rcvDivSize; j++){
CRC[j] = ((CRC[j] == rcvDiv[j])?'0':'1');
}
}
// Move the bits by 1 position for the next computation
for(j=0;j<rcvDivSize-1;j++)
CRC[j]=CRC[j+1];
// appending a bit from data
CRC[j]=rcvData[i++];
}while(i<=rcvDataSize+rcvDivSize-1);
// loop until the data ends
// Append data with check_value(CRC)
for(i=rcvDataSize;i<rcvDataSize+rcvDivSize-1;i++){
rcvData[i]=CRC[i-rcvDataSize];
}
/******************************************************************************************************/
//print the result
printf("CRC is: %s\n",CRC);
printf("Final data: %s\n",rcvData);
//send the result to client
int crcSize,sendDataSize;
crcSize = strlen(CRC);
sendDataSize = strlen(rcvData);
//send the size of data
send(sock,&crcSize,sizeof(int),0);
send(sock,&sendDataSize,sizeof(int),0);
//send the actual data
send(sock,CRC,crcSize*sizeof(char),0);
send(sock,rcvData,sendDataSize*sizeof(char),0);
printf("Data Sent!!!!\n");
close(sock);
return 0;
}
Unlock Success in Your MAKAUT Semester Exam with These Empowering Artificial Intelligence Suggestions! Elevate Your Learning Experience and Excel with Confidence.
Unlock Success in Your MAKAUT Semester Exam with These Empowering Mixed Signal Design Suggestions! Elevate Your Learning Experience and Excel with Confidence.
Unlock Success in Your MAKAUT Semester Exam with These Empowering Fiber Optic Communication Suggestions! Elevate Your Learning Experience and Excel with Confidence.
Unlock Success in Your MAKAUT Semester Exam with These Empowering Cyber Security Suggestions! Elevate Your Learning Experience and Excel with Confidence.
Transform your web design game with my CSS cheatsheet! Master the art of styling, dive into essential properties and selectors, and create visually stunning websites effortlessly. Whether…