Configure a single-client UDP server. The client sends a data word to the server from the user. With the Hamming code error correction technique, the server will find the codeword and return it to the client.
Client-side implementation of single-client UDP server model
// This code is written by SOUVIK GHOSH & DEBMITRA CHATTERJEE
// Client side implementation of UDP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 8080
#define MAXLINE 1024
// Driver code
int main()
{
int sockfd;
char buffer[MAXLINE];
struct sockaddr_in servaddr;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
{
perror("socket creation failed!!!");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = INADDR_ANY;
printf("connected!!\n");
// take data from the user
int n=4;
/*printf("Please enter the size of Dataword: ");
scanf("%d",&n);*/
int arr[n];
printf("Please enter the %d size Dataword: ",n);
for (int i = 0; i <n; i++)
{
scanf("%d",&arr[i]);
}
// send data
sendto(sockfd,&n,sizeof(int),MSG_CONFIRM, (const struct sockaddr *) &servaddr,sizeof(servaddr));
sendto(sockfd,&arr,n*sizeof(int),MSG_CONFIRM, (const struct sockaddr *) &servaddr,sizeof(servaddr));
printf("data sent...\n");
printf("waiting for the server...\n");
// receive data
int size,len;
recvfrom(sockfd,&size,sizeof(int),MSG_WAITALL, (struct sockaddr *) &servaddr, &len);
int receivedBits[size];
recvfrom(sockfd,&receivedBits,size*sizeof(int),MSG_WAITALL, (struct sockaddr *) &servaddr, &len);
printf("data received...\n");
// display received data
for(int i=0;i<size;i++)
{
printf("%d ",receivedBits[i]);
}
printf("\n");
close(sockfd);
return 0;
}
Server-side implementation of single-client UDP server model
// This code is written by SOUVIK GHOSH & DEBMITRA CHATTERJEE
// Server side implementation of UDP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <math.h>
#define PORT 8080
#define MAXLINE 1024
/*********************************************Functions for Hamming code ******************************************************************************/
void hammingCalculation(int data[],int hammingCode[])
{
hammingCode[0]=data[0];
hammingCode[1]=data[1];
hammingCode[2]=data[2];
hammingCode[4]=data[3];
hammingCode[3]=data[0]^data[1]^data[2];
hammingCode[5]=data[0]^data[1]^hammingCode[4]; //data3
hammingCode[6]=data[0]^data[2]^hammingCode[4]; //data3
printf("the encoded bits are: ");
for (int i=0;i<7;i++)
{
printf("%d ",hammingCode[i]);
}
printf("\n");
}
/****************************************************************************************************************************************************/
// main function
int main()
{
int sockfd;
struct sockaddr_in servaddr, cliaddr;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
{
perror("socket creation failed!!");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
// Filling server information
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
// Bind the socket with the server address
if ( bind(sockfd, (const struct sockaddr *)&servaddr,sizeof(servaddr)) < 0 )
{
perror("bind failed!!");
exit(EXIT_FAILURE);
}
printf("server waiting...\n");
int len;
len = sizeof(cliaddr); //len is value/result
//receive data from client
int size;
recvfrom(sockfd,&size,sizeof(int),MSG_WAITALL, (struct sockaddr *) &servaddr, &len);
int receivedBits[size];
int sentBits[7];
int sizeofCode=7;
recvfrom(sockfd,&receivedBits,size*sizeof(int),MSG_WAITALL, (struct sockaddr *) &servaddr,&len);
printf("data received...\n");
// perform operation
// call the hamming calculation function
printf("performing operation...\n");
hammingCalculation(receivedBits,sentBits);
//send data to client
sendto(sockfd,&sizeofCode,sizeof(int),MSG_CONFIRM, (const struct sockaddr *) &servaddr,sizeof(servaddr));
sendto(sockfd,&sentBits,sizeofCode*sizeof(int),MSG_CONFIRM, (const struct sockaddr *) &servaddr,sizeof(servaddr));
printf("data sent...\n");
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…