Skip to content

Commit 086b175

Browse files
authored
Merge pull request #302 from memeghaj10/master
2 parents 7658906 + 2e4526f commit 086b175

File tree

2 files changed

+306
-0
lines changed

2 files changed

+306
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
#include<bits/stdc++.h>
2+
#include<sys/socket.h>
3+
#include<arpa/inet.h>
4+
#include<iostream>
5+
#include<fstream>
6+
#include <sys/types.h>
7+
#include <netinet/in.h>
8+
#include <unistd.h>
9+
10+
11+
using namespace std;
12+
13+
#define MAX 2048
14+
#define port 5200
15+
16+
17+
void flip(int &a) {
18+
a=!a;
19+
}
20+
21+
22+
void revprint(int codeword[],int n) {
23+
for(int i=n-1;i>=0;i--) {
24+
cout<<codeword[i]<<" ";
25+
}
26+
27+
cout<<endl;
28+
}
29+
30+
void decoded(int codeword[]) {
31+
32+
int syndrome = 0;
33+
34+
for(int i=0;i<4;i++) {
35+
int count=0;
36+
37+
for(int j=0;j<12;j++) {
38+
if(((j+1)&(1<<i))) {
39+
count+=codeword[j];
40+
}
41+
}
42+
43+
if(count%2==1) {
44+
syndrome+=(1<<i);
45+
}
46+
47+
}
48+
49+
cout<<"Syndrome : "<<syndrome<<endl;
50+
51+
if(syndrome>=1) {
52+
cout<<"Hence flip bit "<<syndrome<<endl;
53+
flip(codeword[syndrome-1]);
54+
}
55+
56+
57+
int message[8];
58+
59+
int j=7;
60+
for(int i=11;i>=0;i--) {
61+
if((i+1)!=1&&(i+1)!=2&&(i+1)!=4&&(i+1)!=8) {
62+
message[j--]=codeword[i];
63+
}
64+
}
65+
66+
cout<<"Message : ";
67+
revprint(message,8);
68+
69+
int x=0;
70+
for(int i=0;i<8;i++) {
71+
if(message[i]==1) {
72+
x+=(1<<i);
73+
}
74+
}
75+
76+
char c = (char)(x);
77+
cout<<"Character: "<<c<<endl;
78+
79+
}
80+
81+
82+
83+
int main(){
84+
85+
int serverId = socket(AF_INET , SOCK_STREAM , 0); //creating a socket and assigning it to the socket handler
86+
if(serverId < 0){ // socket methode return -1 if the creation was not successful
87+
cout << "Socket creation has failed.";
88+
return 0;
89+
}
90+
91+
struct sockaddr_in serverAddr , clientAddr;
92+
93+
//specifying address and type for the server to operate under
94+
serverAddr.sin_family = AF_INET;
95+
serverAddr.sin_port = htons(port);
96+
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
97+
98+
int bindStatus = bind(serverId , (struct sockaddr*) & serverAddr , sizeof(serverAddr));
99+
100+
if(bindStatus < 0){
101+
cout << "Socket binding has failed" << endl;
102+
return 0;
103+
}
104+
105+
int listenStatus = listen(serverId , 5); //listen to the client while others are waiting in queue of size 5
106+
107+
if(listenStatus < 0){ // when queue is full listen fails
108+
cout << "Listner has failed" << endl;
109+
return 0;
110+
}
111+
112+
cout << "...Waiting for connections \n\n";
113+
114+
int codeword[12];
115+
int clientSocketHandler;
116+
117+
socklen_t len = sizeof(clientAddr);
118+
int connection;
119+
if((connection = accept(serverId , (struct sockaddr*) & clientAddr , &len)) < 0){
120+
cout << "Server didn't accept the request." << endl;
121+
return 0;
122+
}
123+
else{
124+
cout << "Server accepted the request. \n" << endl;
125+
}
126+
127+
int rMsgSize = recv(connection , codeword , MAX , 0);
128+
/*
129+
for(int i=0;i<12;i++) {
130+
cout<<codeword[i]<<" ";
131+
}*/
132+
133+
cout<<"Choose how you want to recieve the message?"<<endl;
134+
cout<<"1) Recieve the original message. "<<endl;
135+
cout<<"2) Recieve message with defect. "<<endl;
136+
137+
int ch;
138+
cin>>ch;
139+
140+
switch(ch) {
141+
case 1:{
142+
cout<<"Recieved Message: ";
143+
revprint(codeword,12);
144+
decoded(codeword);
145+
146+
cout<<"Meesage Recieved Successfully...!"<<endl;
147+
} break;
148+
149+
case 2: {
150+
int x = rand()%12;
151+
flip(codeword[x]);
152+
153+
cout<<"Recieved Message: ";
154+
revprint(codeword,12);
155+
decoded(codeword);
156+
157+
cout<<"Message Recieved Successfully...!"<<endl;
158+
} break;
159+
160+
default : cout<<"Invalid Choice...!"<<endl;
161+
}
162+
163+
164+
close(serverId);
165+
return 0;
166+
167+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#include<bits/stdc++.h>
2+
#include<sys/socket.h>
3+
#include<arpa/inet.h>
4+
#include <sys/types.h>
5+
#include <netinet/in.h>
6+
#include <unistd.h>
7+
8+
using namespace std;
9+
10+
#define MAX 500
11+
#define port 5200
12+
13+
14+
void tobinary(int a[],char c) {
15+
int x=(int)c;
16+
17+
cout<<x<<endl;
18+
19+
for(int i=0;i<8;i++) {
20+
int y = (x&(1<<i));
21+
if(y) {
22+
a[i]=1;
23+
}
24+
25+
else {
26+
a[i]=0;
27+
}
28+
}
29+
30+
}
31+
32+
void revprint(int a[],int n) {
33+
for(int i=n-1;i>=0;i--) {
34+
cout<<a[i]<<" ";
35+
}
36+
cout<<endl;
37+
}
38+
39+
void fillmessageintocodes(int a[], int b[]) {
40+
for(int i=0;i<12;i++) {
41+
a[i]=-1;
42+
}
43+
44+
for(int i=0;i<4;i++) {
45+
a[(1<<i)-1]=0;
46+
}
47+
48+
int j=7;
49+
50+
for(int i=11;i>=0;i--) {
51+
if(a[i]==-1) {
52+
a[i]=b[j];
53+
j--;
54+
}
55+
}
56+
}
57+
58+
59+
int main(){
60+
61+
int clientSocket , serverSocket , receiveMsgSize;
62+
63+
clientSocket = socket(AF_INET , SOCK_STREAM , 0); // creating the socket
64+
65+
if(clientSocket < 0){
66+
cout << "Creation of client socket failed" << endl;
67+
return 0;
68+
}
69+
70+
struct sockaddr_in serverAddr , clientAddr;
71+
72+
// providing socket with IP and port
73+
serverAddr.sin_family = AF_INET;
74+
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
75+
serverAddr.sin_port = htons(port);
76+
77+
if(connect(clientSocket , (struct sockaddr*) & serverAddr , sizeof(serverAddr)) < 0){ // connecting to the receiver
78+
cout << "Connection Error..!" << endl;
79+
return 0;
80+
}
81+
else{
82+
cout << "Connection Established..!" << endl;
83+
}
84+
85+
86+
char c;
87+
cout<<"Enter the First letter of your name: ";
88+
cin>>c;
89+
90+
int binary[8];
91+
tobinary(binary,c);
92+
93+
cout<<"The entered message in binary is: ";
94+
revprint(binary,8);
95+
96+
97+
int codeword[12];
98+
fillmessageintocodes(codeword,binary);
99+
100+
101+
for(int i=0;i<4;i++) {
102+
int count=0;
103+
for(int j=0;j<12;j++) {
104+
if((j+1)&(1<<i)==1) {
105+
count+=codeword[j];
106+
}
107+
}
108+
codeword[(1<<i)-1] = count%2;
109+
}
110+
111+
112+
cout<<"Finally sent codeword: ";
113+
revprint(codeword,12);
114+
115+
116+
send(clientSocket , codeword , sizeof(codeword), 0); // sending the codeword
117+
118+
//revprint(codeword,12);
119+
120+
cout << "Message Sent Successfully...!" << endl;
121+
122+
close(clientSocket); // closing the socket
123+
124+
return 0;
125+
}
126+
127+
128+
129+
130+
131+
132+
133+
134+
135+
136+
137+
138+
139+

0 commit comments

Comments
 (0)