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 codeintmain(){int sockfd;char buffer[MAXLINE];struct sockaddr_in servaddr;// Creating socket file descriptorif((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 userint 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 datasendto(sockfd,&n,sizeof(int),MSG_CONFIRM,(conststruct sockaddr *)&servaddr,sizeof(servaddr));sendto(sockfd,&arr,n*sizeof(int),MSG_CONFIRM,(conststruct sockaddr *)&servaddr,sizeof(servaddr));printf("data sent...\n");printf("waiting for the server...\n");// receive dataint 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 datafor(int i=0;i<size;i++){printf("%d ",receivedBits[i]);}printf("\n");close(sockfd);return0;}
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 ******************************************************************************/voidhammingCalculation(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];//data3printf("the encoded bits are: ");for(int i=0;i<7;i++){printf("%d ",hammingCode[i]);}printf("\n");}/****************************************************************************************************************************************************/// main functionintmain(){int sockfd;struct sockaddr_in servaddr, cliaddr;// Creating socket file descriptorif((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 addressif(bind(sockfd,(conststruct 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 clientint 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 functionprintf("performing operation...\n");hammingCalculation(receivedBits,sentBits);//send data to clientsendto(sockfd,&sizeofCode,sizeof(int),MSG_CONFIRM,(conststruct sockaddr *)&servaddr,sizeof(servaddr));sendto(sockfd,&sentBits,sizeofCode*sizeof(int),MSG_CONFIRM,(conststruct sockaddr *)&servaddr,sizeof(servaddr));printf("data sent...\n");return0;}
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…