-
Notifications
You must be signed in to change notification settings - Fork 340
Adding Hamming Code #302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Adding Hamming Code #302
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
added Hamming Code
- Loading branch information
commit 96bf7af9ec99d172010f4a680f234afef9f59a5c
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Hamming Code | ||
|
||
Sender Side: Input is the first character of your name or any other character you like. | ||
|
||
Reciever Side: Recieved codeword using TCP Socket. | ||
|
||
The reciever also serves the following functionalities:- | ||
a) Prints Data recieved without error. | ||
b) Prints Data recieved with error. | ||
|
167 changes: 167 additions & 0 deletions
167
algorithms/Networks/Hamming_Code/hamming_code_reciever.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
#include<bits/stdc++.h> | ||
#include<sys/socket.h> | ||
#include<arpa/inet.h> | ||
#include<iostream> | ||
#include<fstream> | ||
#include <sys/types.h> | ||
#include <netinet/in.h> | ||
#include <unistd.h> | ||
|
||
|
||
using namespace std; | ||
|
||
#define MAX 2048 | ||
#define port 5200 | ||
|
||
|
||
void flip(int &a) { | ||
a=!a; | ||
} | ||
|
||
|
||
void revprint(int codeword[],int n) { | ||
for(int i=n-1;i>=0;i--) { | ||
cout<<codeword[i]<<" "; | ||
} | ||
|
||
cout<<endl; | ||
} | ||
|
||
void decoded(int codeword[]) { | ||
|
||
int syndrome = 0; | ||
|
||
for(int i=0;i<4;i++) { | ||
int count=0; | ||
|
||
for(int j=0;j<12;j++) { | ||
if(((j+1)&(1<<i))) { | ||
count+=codeword[j]; | ||
} | ||
} | ||
|
||
if(count%2==1) { | ||
syndrome+=(1<<i); | ||
} | ||
|
||
} | ||
|
||
cout<<"Syndrome : "<<syndrome<<endl; | ||
|
||
if(syndrome>=1) { | ||
cout<<"Hence flip bit "<<syndrome<<endl; | ||
flip(codeword[syndrome-1]); | ||
} | ||
|
||
|
||
int message[8]; | ||
|
||
int j=7; | ||
for(int i=11;i>=0;i--) { | ||
if((i+1)!=1&&(i+1)!=2&&(i+1)!=4&&(i+1)!=8) { | ||
message[j--]=codeword[i]; | ||
} | ||
} | ||
|
||
cout<<"Message : "; | ||
revprint(message,8); | ||
|
||
int x=0; | ||
for(int i=0;i<8;i++) { | ||
if(message[i]==1) { | ||
x+=(1<<i); | ||
} | ||
} | ||
|
||
char c = (char)(x); | ||
cout<<"Character: "<<c<<endl; | ||
|
||
} | ||
|
||
|
||
|
||
int main(){ | ||
|
||
int serverId = socket(AF_INET , SOCK_STREAM , 0); //creating a socket and assigning it to the socket handler | ||
if(serverId < 0){ // socket methode return -1 if the creation was not successful | ||
cout << "Socket creation has failed."; | ||
return 0; | ||
} | ||
|
||
struct sockaddr_in serverAddr , clientAddr; | ||
|
||
//specifying address and type for the server to operate under | ||
serverAddr.sin_family = AF_INET; | ||
serverAddr.sin_port = htons(port); | ||
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); | ||
|
||
int bindStatus = bind(serverId , (struct sockaddr*) & serverAddr , sizeof(serverAddr)); | ||
|
||
if(bindStatus < 0){ | ||
cout << "Socket binding has failed" << endl; | ||
return 0; | ||
} | ||
|
||
int listenStatus = listen(serverId , 5); //listen to the client while others are waiting in queue of size 5 | ||
|
||
if(listenStatus < 0){ // when queue is full listen fails | ||
cout << "Listner has failed" << endl; | ||
return 0; | ||
} | ||
|
||
cout << "...Waiting for connections \n\n"; | ||
|
||
int codeword[12]; | ||
int clientSocketHandler; | ||
|
||
socklen_t len = sizeof(clientAddr); | ||
int connection; | ||
if((connection = accept(serverId , (struct sockaddr*) & clientAddr , &len)) < 0){ | ||
cout << "Server didn't accept the request." << endl; | ||
return 0; | ||
} | ||
else{ | ||
cout << "Server accepted the request. \n" << endl; | ||
} | ||
|
||
int rMsgSize = recv(connection , codeword , MAX , 0); | ||
/* | ||
for(int i=0;i<12;i++) { | ||
cout<<codeword[i]<<" "; | ||
}*/ | ||
|
||
cout<<"Choose how you want to recieve the message?"<<endl; | ||
cout<<"1) Recieve the original message. "<<endl; | ||
cout<<"2) Recieve message with defect. "<<endl; | ||
|
||
int ch; | ||
cin>>ch; | ||
|
||
switch(ch) { | ||
case 1:{ | ||
cout<<"Recieved Message: "; | ||
revprint(codeword,12); | ||
decoded(codeword); | ||
|
||
cout<<"Meesage Recieved Successfully...!"<<endl; | ||
} break; | ||
|
||
case 2: { | ||
int x = rand()%12; | ||
flip(codeword[x]); | ||
|
||
cout<<"Recieved Message: "; | ||
revprint(codeword,12); | ||
decoded(codeword); | ||
|
||
cout<<"Message Recieved Successfully...!"<<endl; | ||
} break; | ||
|
||
default : cout<<"Invalid Choice...!"<<endl; | ||
} | ||
|
||
|
||
close(serverId); | ||
return 0; | ||
|
||
} |
139 changes: 139 additions & 0 deletions
139
algorithms/Networks/Hamming_Code/hamming_code_sender.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#include<bits/stdc++.h> | ||
#include<sys/socket.h> | ||
#include<arpa/inet.h> | ||
#include <sys/types.h> | ||
#include <netinet/in.h> | ||
#include <unistd.h> | ||
|
||
using namespace std; | ||
|
||
#define MAX 500 | ||
#define port 5200 | ||
|
||
|
||
void tobinary(int a[],char c) { | ||
int x=(int)c; | ||
|
||
cout<<x<<endl; | ||
|
||
for(int i=0;i<8;i++) { | ||
int y = (x&(1<<i)); | ||
if(y) { | ||
a[i]=1; | ||
} | ||
|
||
else { | ||
a[i]=0; | ||
} | ||
} | ||
|
||
} | ||
|
||
void revprint(int a[],int n) { | ||
for(int i=n-1;i>=0;i--) { | ||
cout<<a[i]<<" "; | ||
} | ||
cout<<endl; | ||
} | ||
|
||
void fillmessageintocodes(int a[], int b[]) { | ||
for(int i=0;i<12;i++) { | ||
a[i]=-1; | ||
} | ||
|
||
for(int i=0;i<4;i++) { | ||
a[(1<<i)-1]=0; | ||
} | ||
|
||
int j=7; | ||
|
||
for(int i=11;i>=0;i--) { | ||
if(a[i]==-1) { | ||
a[i]=b[j]; | ||
j--; | ||
} | ||
} | ||
} | ||
|
||
|
||
int main(){ | ||
|
||
int clientSocket , serverSocket , receiveMsgSize; | ||
|
||
clientSocket = socket(AF_INET , SOCK_STREAM , 0); // creating the socket | ||
|
||
if(clientSocket < 0){ | ||
cout << "Creation of client socket failed" << endl; | ||
return 0; | ||
} | ||
|
||
struct sockaddr_in serverAddr , clientAddr; | ||
|
||
// providing socket with IP and port | ||
serverAddr.sin_family = AF_INET; | ||
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); | ||
serverAddr.sin_port = htons(port); | ||
|
||
if(connect(clientSocket , (struct sockaddr*) & serverAddr , sizeof(serverAddr)) < 0){ // connecting to the receiver | ||
cout << "Connection Error..!" << endl; | ||
return 0; | ||
} | ||
else{ | ||
cout << "Connection Established..!" << endl; | ||
} | ||
|
||
|
||
char c; | ||
cout<<"Enter the First letter of your name: "; | ||
cin>>c; | ||
|
||
int binary[8]; | ||
tobinary(binary,c); | ||
|
||
cout<<"The entered message in binary is: "; | ||
revprint(binary,8); | ||
|
||
|
||
int codeword[12]; | ||
fillmessageintocodes(codeword,binary); | ||
|
||
|
||
for(int i=0;i<4;i++) { | ||
int count=0; | ||
for(int j=0;j<12;j++) { | ||
if((j+1)&(1<<i)==1) { | ||
count+=codeword[j]; | ||
} | ||
} | ||
codeword[(1<<i)-1] = count%2; | ||
} | ||
|
||
|
||
cout<<"Finally sent codeword: "; | ||
revprint(codeword,12); | ||
|
||
|
||
send(clientSocket , codeword , sizeof(codeword), 0); // sending the codeword | ||
|
||
//revprint(codeword,12); | ||
|
||
cout << "Message Sent Successfully...!" << endl; | ||
|
||
close(clientSocket); // closing the socket | ||
|
||
return 0; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.