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 redundant bits
void calculateRedundantBits(char dataword[], char redundantBits[]) {
int d[7], p1, p2, p4, p8;
int i, d_len = strlen(dataword);

// Ensure the dataword is 7 bits long
if (d_len != 7) {
printf("Error: Dataword must be 7 bits long!n");
strcpy(redundantBits, "ERROR");
return;
}

// Convert dataword characters to integer array
for (i = 0; i < 7; i++) {
d[i] = dataword[i] - '0';
}

// Compute redundant bits using Hamming Code (for 7-bit dataword)
p1 = d[0] ^ d[1] ^ d[3] ^ d[4] ^ d[6]; // Parity bit 1
p2 = d[0] ^ d[2] ^ d[3] ^ d[5] ^ d[6]; // Parity bit 2
p4 = d[1] ^ d[2] ^ d[3]; // Parity bit 4
p8 = d[4] ^ d[5] ^ d[6]; // Parity bit 8

// Store redundant bits as a string
sprintf(redundantBits, "%d%d%d%d", p1, p2, p4, p8);
}

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

// 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);

// Compute redundant bits
calculateRedundantBits(dataword, redundantBits);

// Print only redundant bits
printf("Redundant Bits: %sn", redundantBits);

// Close sockets
close(client_fd);
close(server_fd);
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.