NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 8080
#define MAX_SIZE 1024

// Function to calculate Hamming Code
void generateHammingCode(char dataword[], char codeword[]) {
int d[16], p[5], hamming[21];
int i, j, k, d_len = strlen(dataword);

// Convert dataword from char to int array
for (i = 0; i < d_len; i++)
d[i] = dataword[i] - '0';

// Calculate positions of parity bits
p[0] = d[0] ^ d[1] ^ d[3] ^ d[4] ^ d[6]; // P1
p[1] = d[0] ^ d[2] ^ d[3] ^ d[5] ^ d[6]; // P2
p[2] = d[1] ^ d[2] ^ d[3]; // P4
p[3] = d[4] ^ d[5] ^ d[6]; // P8

// Constructing final codeword with parity bits at 1, 2, 4, 8
hamming[0] = p[0]; // P1
hamming[1] = p[1]; // P2
hamming[2] = d[0];
hamming[3] = p[2]; // P4
hamming[4] = d[1];
hamming[5] = d[2];
hamming[6] = d[3];
hamming[7] = p[3]; // P8
hamming[8] = d[4];
hamming[9] = d[5];
hamming[10] = d[6];

// Convert hamming array to string
for (i = 0; i < 11; i++)
codeword[i] = hamming[i] + '0';
codeword[11] = '';
}

int main() {
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_size;
char dataword[MAX_SIZE], codeword[MAX_SIZE];

// Create socket
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
perror("Socket creation failed");
exit(1);
}

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;

// Bind socket to the port
if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("Binding failed");
exit(1);
}

// Listen for client connections
if (listen(server_fd, 1) == -1) {
perror("Listening failed");
exit(1);
}

printf("Server is listening on port %d...n", PORT);

addr_size = sizeof(client_addr);
client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &addr_size);
if (client_fd == -1) {
perror("Client connection failed");
exit(1);
}

// Receive dataword from client
recv(client_fd, dataword, MAX_SIZE, 0);
printf("Received Dataword: %sn", dataword);

// Generate Hamming Code
generateHammingCode(dataword, codeword);
printf("Hamming Code (Dataword + Redundant Bits): %sn", codeword);

// Close the sockets
close(client_fd);
close(server_fd);
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 8080
#define MAX_SIZE 1024

int main() {
int sock;
struct sockaddr_in server_addr;
char dataword[MAX_SIZE];

// Create socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
perror("Socket creation failed");
exit(1);
}

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;

// Connect to server
if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("Connection failed");
exit(1);
}

printf("Enter binary dataword: ");
scanf("%s", dataword);

// Send dataword to server
send(sock, dataword, strlen(dataword), 0);
printf("Dataword sent to server.n");

// Close the socket
close(sock);
return 0;
}
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.