C++ Implementation Examples
Error detection and correction algorithms for reliable data transmission
Implements Hamming code error detection and correction. Encodes 4-bit data into 7-bit Hamming code using parity bits at positions 1, 2, and 4. Can detect and correct single-bit errors in received data.
g++ hamming.cpp -o hamming./hamming1 0 1 11 0 1 1 0 0 1 (introduces error at position 1)#include <iostream>
#include <vector>
using namespace std;
void calculateParity(vector<int>& hamming) {
hamming[0] = hamming[2] ^ hamming[4] ^ hamming[6];
hamming[1] = hamming[2] ^ hamming[5] ^ hamming[6];
hamming[3] = hamming[4] ^ hamming[5] ^ hamming[6];
}
int findError(vector<int>& hamming) {
int p1 = hamming[0] ^ hamming[2] ^ hamming[4] ^ hamming[6];
int p2 = hamming[1] ^ hamming[2] ^ hamming[5] ^ hamming[6];
int p4 = hamming[3] ^ hamming[4] ^ hamming[5] ^ hamming[6];
return p1 + 2*p2 + 4*p4;
}
int main() {
vector<int> data(4);
cout << "Enter 4 data bits: ";
for(int i = 0; i < 4; i++) {
cin >> data[i];
}
vector<int> hamming(7);
hamming[2] = data[0];
hamming[4] = data[1];
hamming[5] = data[2];
hamming[6] = data[3];
calculateParity(hamming);
cout << "Hamming code: ";
for(int bit : hamming) {
cout << bit << " ";
}
cout << endl;
cout << "Enter received 7-bit code: ";
for(int i = 0; i < 7; i++) {
cin >> hamming[i];
}
int errorPos = findError(hamming);
if(errorPos == 0) {
cout << "No error detected" << endl;
} else {
cout << "Error at position: " << errorPos << endl;
hamming[errorPos-1] ^= 1;
cout << "Corrected code: ";
for(int bit : hamming) {
cout << bit << " ";
}
cout << endl;
}
return 0;
}
Implements Cyclic Redundancy Check (CRC) for error detection. Performs modulo-2 division using XOR operations to generate CRC codeword and detect errors in received data.
g++ crc.cpp -o crc./crc101011011010110 (introduces error)#include <iostream>
#include <string>
using namespace std;
string xorOperation(string a, string b) {
string result = "";
for(int i = 1; i < b.length(); i++) {
if(a[i] == b[i]) {
result += "0";
} else {
result += "1";
}
}
return result;
}
string modulo2Division(string dividend, string divisor) {
int pick = divisor.length();
string tmp = dividend.substr(0, pick);
while(pick < dividend.length()) {
if(tmp[0] == '1') {
tmp = xorOperation(divisor, tmp) + dividend[pick];
} else {
tmp = xorOperation(string(divisor.length(), '0'), tmp) + dividend[pick];
}
pick++;
}
if(tmp[0] == '1') {
tmp = xorOperation(divisor, tmp);
} else {
tmp = xorOperation(string(divisor.length(), '0'), tmp);
}
return tmp;
}
int main() {
string data, generator;
cout << "Enter data bits: ";
cin >> data;
cout << "Enter generator polynomial: ";
cin >> generator;
string codeword = data + string(generator.length() - 1, '0');
string remainder = modulo2Division(codeword, generator);
codeword = data + remainder;
cout << "CRC codeword: " << codeword << endl;
cout << "Enter received codeword: ";
cin >> codeword;
remainder = modulo2Division(codeword, generator);
bool hasError = false;
for(char bit : remainder) {
if(bit == '1') {
hasError = true;
break;
}
}
if(hasError) {
cout << "Error detected!" << endl;
} else {
cout << "No error detected" << endl;
}
return 0;
}
Network programming examples using TCP and UDP protocols
TCP echo server that listens on port 8080, accepts client connections, and echoes back received messages. Uses reliable TCP connection for communication.
g++ tcp_server.cpp -o tcp_server./tcp_server./tcp_server./tcp_client#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
using namespace std;
int main() {
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8080);
serverAddr.sin_addr.s_addr = INADDR_ANY;
bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
listen(serverSocket, 5);
cout << "TCP Server listening on port 8080..." << endl;
int clientSocket = accept(serverSocket, NULL, NULL);
char buffer[1024];
while(true) {
memset(buffer, 0, sizeof(buffer));
int bytesRead = recv(clientSocket, buffer, sizeof(buffer), 0);
if(bytesRead <= 0) break;
cout << "Received: " << buffer << endl;
send(clientSocket, buffer, strlen(buffer), 0);
}
close(clientSocket);
close(serverSocket);
return 0;
}
TCP client that connects to server on localhost:8080, sends messages and receives echoed responses. Provides interactive interface for message exchange.
g++ tcp_client.cpp -o tcp_client./tcp_clientHello ServerThis is a test messagequit (to exit)
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
using namespace std;
int main() {
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8080);
inet_pton(AF_INET, "127.0.0.1", &serverAddr.sin_addr);
connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
char buffer[1024];
string message;
while(true) {
cout << "Enter message (quit to exit): ";
getline(cin, message);
if(message == "quit") break;
send(clientSocket, message.c_str(), message.length(), 0);
memset(buffer, 0, sizeof(buffer));
recv(clientSocket, buffer, sizeof(buffer), 0);
cout << "Echo: " << buffer << endl;
}
close(clientSocket);
return 0;
}
UDP echo server that listens on port 8080 and echoes back messages from any UDP client. Uses connectionless UDP protocol for communication.
g++ udp_server.cpp -o udp_server./udp_server./udp_server./udp_client#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
using namespace std;
int main() {
int serverSocket = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in serverAddr, clientAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8080);
serverAddr.sin_addr.s_addr = INADDR_ANY;
bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
cout << "UDP Server listening on port 8080..." << endl;
char buffer[1024];
socklen_t clientLen = sizeof(clientAddr);
while(true) {
memset(buffer, 0, sizeof(buffer));
recvfrom(serverSocket, buffer, sizeof(buffer), 0,
(struct sockaddr*)&clientAddr, &clientLen);
cout << "Received: " << buffer << endl;
sendto(serverSocket, buffer, strlen(buffer), 0,
(struct sockaddr*)&clientAddr, clientLen);
}
close(serverSocket);
return 0;
}
UDP client that sends messages to server on localhost:8080 and receives echoed responses. Uses connectionless UDP protocol with interactive interface.
g++ udp_client.cpp -o udp_client./udp_clientHello UDP ServerUDP Message Testquit (to exit)
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
using namespace std;
int main() {
int clientSocket = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8080);
inet_pton(AF_INET, "127.0.0.1", &serverAddr.sin_addr);
char buffer[1024];
string message;
socklen_t serverLen = sizeof(serverAddr);
while(true) {
cout << "Enter message (quit to exit): ";
getline(cin, message);
if(message == "quit") break;
sendto(clientSocket, message.c_str(), message.length(), 0,
(struct sockaddr*)&serverAddr, serverLen);
memset(buffer, 0, sizeof(buffer));
recvfrom(clientSocket, buffer, sizeof(buffer), 0, NULL, NULL);
cout << "Echo: " << buffer << endl;
}
close(clientSocket);
return 0;
}
Multi-client TCP chat server using select() for handling multiple clients simultaneously. Broadcasts messages from one client to all other connected clients.
g++ chat_server.cpp -o chat_server./chat_server./chat_servertelnet localhost 8080telnet localhost 8080#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <sys/select.h>
using namespace std;
int main() {
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
int clients[10] = {0};
int maxClients = 10;
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8080);
serverAddr.sin_addr.s_addr = INADDR_ANY;
bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
listen(serverSocket, 5);
cout << "Chat Server listening on port 8080..." << endl;
fd_set readfds;
char buffer[1024];
while(true) {
FD_ZERO(&readfds);
FD_SET(serverSocket, &readfds);
int maxfd = serverSocket;
for(int i = 0; i < maxClients; i++) {
if(clients[i] > 0) {
FD_SET(clients[i], &readfds);
if(clients[i] > maxfd) maxfd = clients[i];
}
}
select(maxfd + 1, &readfds, NULL, NULL, NULL);
if(FD_ISSET(serverSocket, &readfds)) {
int newClient = accept(serverSocket, NULL, NULL);
for(int i = 0; i < maxClients; i++) {
if(clients[i] == 0) {
clients[i] = newClient;
cout << "New client connected: " << newClient << endl;
break;
}
}
}
for(int i = 0; i < maxClients; i++) {
if(clients[i] > 0 && FD_ISSET(clients[i], &readfds)) {
memset(buffer, 0, sizeof(buffer));
int bytesRead = recv(clients[i], buffer, sizeof(buffer), 0);
if(bytesRead <= 0) {
close(clients[i]);
clients[i] = 0;
} else {
for(int j = 0; j < maxClients; j++) {
if(clients[j] > 0 && clients[j] != clients[i]) {
send(clients[j], buffer, strlen(buffer), 0);
}
}
}
}
}
}
return 0;
}
IP address analysis and subnet calculations
Analyzes IP addresses and determines their class (A, B, C, D, E) based on the first octet. Displays network address, default subnet mask, and host range for each class.
g++ classful.cpp -o classful./classful10.0.0.1172.16.0.1192.168.1.100224.0.0.1
#include <iostream>
#include <string>
using namespace std;
void parseIP(string ip, int& a, int& b, int& c, int& d) {
sscanf(ip.c_str(), "%d.%d.%d.%d", &a, &b, &c, &d);
}
int main() {
string ip;
cout << "Enter IP address: ";
cin >> ip;
int a, b, c, d;
parseIP(ip, a, b, c, d);
if(a >= 1 && a <= 126) {
cout << "Class A" << endl;
cout << "Network: " << a << ".0.0.0" << endl;
cout << "Default mask: 255.0.0.0" << endl;
cout << "Host range: " << a << ".0.0.1 to " << a << ".255.255.254" << endl;
}
else if(a >= 128 && a <= 191) {
cout << "Class B" << endl;
cout << "Network: " << a << "." << b << ".0.0" << endl;
cout << "Default mask: 255.255.0.0" << endl;
cout << "Host range: " << a << "." << b << ".0.1 to " << a << "." << b << ".255.254" << endl;
}
else if(a >= 192 && a <= 223) {
cout << "Class C" << endl;
cout << "Network: " << a << "." << b << "." << c << ".0" << endl;
cout << "Default mask: 255.255.255.0" << endl;
cout << "Host range: " << a << "." << b << "." << c << ".1 to " << a << "." << b << "." << c << ".254" << endl;
}
else if(a >= 224 && a <= 239) {
cout << "Class D (Multicast)" << endl;
}
else {
cout << "Class E (Reserved)" << endl;
}
return 0;
}
Processes CIDR notation addresses and calculates network information including network address, subnet mask, broadcast address, host range, and total available hosts.
g++ cidr.cpp -o cidr./cidr192.168.1.0/24 (Class C with /24)10.0.0.0/8 (Class A with /8)172.16.0.0/16 (Class B with /16)192.168.0.0/25 (Subnet with /25)
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
void parseIP(string ip, int& a, int& b, int& c, int& d) {
sscanf(ip.c_str(), "%d.%d.%d.%d", &a, &b, &c, &d);
}
unsigned int ipToInt(int a, int b, int c, int d) {
return (a << 24) | (b << 16) | (c << 8) | d;
}
void intToIP(unsigned int ip, int& a, int& b, int& c, int& d) {
a = (ip >> 24) & 0xFF;
b = (ip >> 16) & 0xFF;
c = (ip >> 8) & 0xFF;
d = ip & 0xFF;
}
int main() {
string cidr;
cout << "Enter CIDR notation (e.g., 192.168.1.0/24): ";
cin >> cidr;
size_t pos = cidr.find('/');
string ip = cidr.substr(0, pos);
int prefix = stoi(cidr.substr(pos + 1));
int a, b, c, d;
parseIP(ip, a, b, c, d);
unsigned int mask = 0xFFFFFFFF << (32 - prefix);
unsigned int ipAddr = ipToInt(a, b, c, d);
unsigned int network = ipAddr & mask;
unsigned int broadcast = network | (~mask);
int hostBits = 32 - prefix;
int totalHosts = pow(2, hostBits) - 2;
int na, nb, nc, nd, ba, bb, bc, bd, ma, mb, mc, md;
intToIP(network, na, nb, nc, nd);
intToIP(broadcast, ba, bb, bc, bd);
intToIP(mask, ma, mb, mc, md);
cout << "Network: " << na << "." << nb << "." << nc << "." << nd << endl;
cout << "Subnet mask: " << ma << "." << mb << "." << mc << "." << md << endl;
cout << "Broadcast: " << ba << "." << bb << "." << bc << "." << bd << endl;
cout << "Host range: " << na << "." << nb << "." << nc << "." << (nd+1)
<< " to " << ba << "." << bb << "." << bc << "." << (bd-1) << endl;
cout << "Total hosts: " << totalHosts << endl;
return 0;
}
Performs subnet calculation by dividing a network into smaller subnets. Calculates new prefix length and displays network details for each subnet including addresses and host ranges.
g++ subnetting.cpp -o subnetting./subnetting192.168.0.0244#include <iostream>
#include <string>
#include <cmath>
using namespace std;
void parseIP(string ip, int& a, int& b, int& c, int& d) {
sscanf(ip.c_str(), "%d.%d.%d.%d", &a, &b, &c, &d);
}
unsigned int ipToInt(int a, int b, int c, int d) {
return (a << 24) | (b << 16) | (c << 8) | d;
}
void intToIP(unsigned int ip, int& a, int& b, int& c, int& d) {
a = (ip >> 24) & 0xFF;
b = (ip >> 16) & 0xFF;
c = (ip >> 8) & 0xFF;
d = ip & 0xFF;
}
int main() {
string ip;
int originalPrefix, subnets;
cout << "Enter network (e.g., 192.168.1.0): ";
cin >> ip;
cout << "Enter original prefix length: ";
cin >> originalPrefix;
cout << "Enter number of subnets needed: ";
cin >> subnets;
int subnetBits = ceil(log2(subnets));
int newPrefix = originalPrefix + subnetBits;
if(newPrefix > 30) {
cout << "Too many subnets for this network!" << endl;
return 1;
}
int a, b, c, d;
parseIP(ip, a, b, c, d);
unsigned int baseNetwork = ipToInt(a, b, c, d);
unsigned int subnetSize = 1 << (32 - newPrefix);
cout << "\nSubnet Information:" << endl;
cout << "New prefix length: /" << newPrefix << endl;
cout << "Hosts per subnet: " << (subnetSize - 2) << endl;
for(int i = 0; i < subnets; i++) {
unsigned int subnetAddr = baseNetwork + (i * subnetSize);
unsigned int broadcastAddr = subnetAddr + subnetSize - 1;
int sa, sb, sc, sd, ba, bb, bc, bd;
intToIP(subnetAddr, sa, sb, sc, sd);
intToIP(broadcastAddr, ba, bb, bc, bd);
cout << "\nSubnet " << (i+1) << ":" << endl;
cout << "Network: " << sa << "." << sb << "." << sc << "." << sd << "/" << newPrefix << endl;
cout << "Broadcast: " << ba << "." << bb << "." << bc << "." << bd << endl;
cout << "Host range: " << sa << "." << sb << "." << sc << "." << (sd+1)
<< " to " << ba << "." << bb << "." << bc << "." << (bd-1) << endl;
}
return 0;
}