UDP_Server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <math.h>
int main(){
int sid = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in s, c;
s.sin_family = AF_INET;
s.sin_port = htons(6666);
s.sin_addr.s_addr = inet_addr("127.0.0.1");
bind(sid, (struct sockaddr *)&s, sizeof(s));
int l = sizeof(c);
while(1){
char arr[100000];
char brr[100000] = {'0'};
int n = recvfrom(sid, (void *)arr, sizeof(arr), 0, (struct sockaddr *)&c, &l) - 1;
printf("Dataword: %s\n", arr);
int i,j,r = 0;
for(i=1;;i++){
if((int)pow(2,i)-i > n){
r = i;
break;
}
}
int c1=0;
for(i=0;i<n+r;i++)
{
float p=log2(i+1);
if(p==(int)p)
continue;
brr[i]=arr[c1++];
}
for(i=1;i<=n+r;i*=2)
{
int count=0;
j=i-1;
while(j<n+r)
{
int cur=j;
for(j=cur;j<(cur+i);j++)
{
if(j>=(n+r))
break;
if(brr[j]=='1')
count++;
}
j+=i;
}
if(count%2==1)
brr[i-1]='1';
else
brr[i-1]='0';
}
brr[n+r]='\0';
printf("Codeword: %s\n",brr);
sendto(sid, (void *)brr, strlen(brr)+1, 0, (struct sockaddr *)&c, sizeof(c));
}
return 0;
}
UDP_Client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
int main(){
int sid = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in s;
s.sin_family = AF_INET;
s.sin_port = htons(6666);
s.sin_addr.s_addr = inet_addr("127.0.0.1");
//bind(sid, (struct sockaddr *)&s, sizeof(s));
char arr[100000];
printf("Enter a binary string:\n");
scanf("%s", arr);
sendto(sid, (void *)arr, strlen(arr)+1, 0, (struct sockaddr *)&s, sizeof(s));
int l = sizeof(s);
recvfrom(sid, (void *)arr, sizeof(arr), 0, (struct sockaddr *)&s, &l);
printf("%s\n", arr);
return 0;
}
Post a Comment