0% found this document useful (0 votes)
2 views17 pages

Cn Chatgpt Programs

The document contains multiple C/C++ code snippets demonstrating various algorithms and networking concepts. It includes implementations of Dijkstra's algorithm for shortest paths, CRC error detection, distance vector routing, client-server communication using sockets, Hamming code for error correction, and FIFO communication. Each section is a standalone program showcasing different functionalities in computer networking and algorithm design.

Uploaded by

aksh.a55t
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views17 pages

Cn Chatgpt Programs

The document contains multiple C/C++ code snippets demonstrating various algorithms and networking concepts. It includes implementations of Dijkstra's algorithm for shortest paths, CRC error detection, distance vector routing, client-server communication using sockets, Hamming code for error correction, and FIFO communication. Each section is a standalone program showcasing different functionalities in computer networking and algorithm design.

Uploaded by

aksh.a55t
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

1.

#include<iostream>
using namespace std;

class Dj {
int n, cost[10][10], d[10], p[10], v[10];
public:
void readMatrix();
void shortestPath(int src);
void display(int src);
};

void Dj::readMatrix() {
cout << "Enter the number of vertices: ";
cin >> n;
cout << "Enter the cost adjacency matrix:\n";
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
cin >> cost[i][j];
}

void Dj::shortestPath(int src) {


for (int i = 0; i < n; ++i) {
d[i] = cost[src][i];
p[i] = src;
v[i] = 0;
}
v[src] = 1;

for (int i = 0; i < n - 1; ++i) {


int min = 99, u;
for (int j = 0; j < n; ++j)
if (!v[j] && d[j] < min)
min = d[j], u = j;

v[u] = 1;
for (int s = 0; s < n; ++s)
if (!v[s] && d[u] + cost[u][s] < d[s])
d[s] = d[u] + cost[u][s], p[s] = u;
}
}

void Dj::display(int src) {


for (int i = 0; i < n; ++i) {
if (i == src) continue;
cout << "The shortest path from " << src << " to " << i << " is: ";
for (int k = i; k != src; k = p[k])
cout << k << " <- ";
cout << src << "\nand the distance is " << d[i] << endl;
}
}

int main() {
Dj dij;
dij.readMatrix();
int source;
cout << "Enter the source: ";
cin >> source;
dij.shortestPath(source);
dij.display(source);
return 0;
}

2.

#include <stdio.h>
#include <string.h>

char data[100], concatdata[117], src_crc[17], dest_crc[17], frame[120], divident[18];


const char divisor[18] = "10001000000100001";
const char res[17] = "0000000000000000";

void crc_cal(int node) {


int i, j;
for (j = 17; j <= strlen(node == 0 ? concatdata : frame); j++) {
if (divident[0] == '1') {
for (i = 1; i <= 16; i++)
divident[i - 1] = divident[i] == divisor[i] ? '0' : '1';
} else {
for (i = 1; i <= 16; i++)
divident[i - 1] = divident[i];
}
divident[i - 1] = node == 0 ? concatdata[j] : frame[j];
}
divident[i - 1] = '\0';
if (node == 0) strcpy(src_crc, divident);
else strcpy(dest_crc, divident);
}

int main() {
printf("\n\t\t\tAT SOURCE NODE\n\nEnter the data to be sent: ");
gets(data);
strcpy(concatdata, data);
strcat(concatdata, "0000000000000000");
strncpy(divident, concatdata, 17);
divident[17] = '\0';
crc_cal(0);

printf("\nData: %s\nFrame transmitted: %s%s\n", data, data, src_crc);


printf("\n\t\tSOURCE NODE TRANSMITTED THE FRAME ---->");
printf("\n\n\n\n\t\t\tAT DESTINATION NODE\nEnter the received frame: ");
gets(frame);
strncpy(divident, frame, 17);
divident[17] = '\0';
crc_cal(1);

if (strcmp(dest_crc, res) == 0)
printf("\nReceived frame is error-free.\n");
else
printf("\nReceived frame has one or more errors.\n");

return 0;
}

3.

#include <stdio.h>

struct rtable {
int dist[20], nextnode[20];
} table[20];

int cost[10][10], n;

void distvector() {
int i, j, k, count;
do {
count = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k < n; k++) {
int new_dist = cost[i][k] + table[k].dist[j];
if (table[i].dist[j] > new_dist) {
table[i].dist[j] = new_dist;
table[i].nextnode[j] = k;
count++;
}
}
}
}
} while (count);
}

int main() {
printf("Enter the number of vertices: ");
scanf("%d", &n);

printf("Enter the cost matrix:\n");


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &cost[i][j]);
table[i].dist[j] = cost[i][j];
table[i].nextnode[j] = j;
}
}

distvector();

for (int i = 0; i < n; i++) {


printf("\nState value for router %c\nDestnode\tNextnode\tDistance\n", i + 65);
for (int j = 0; j < n; j++) {
if (table[i].dist[j] == 99) {
printf("%c\t\t-\t\tInfinite\n", j + 65);
} else {
printf("%c\t\t%c\t\t%d\n", j + 65, table[i].nextnode[j] + 65, table[i].dist[j]);
}
}
}
return 0;
}

4.

SERVER:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

void error(const char *msg) {


perror(msg);
exit(1);
}
int main(int argc, char *argv[]) {
int sockfd, newsockfd, portno, clilen;
char buffer[256], c[2000];
struct sockaddr_in serv_addr, cli_addr;
FILE *fd;

if (argc < 2) {
fprintf(stderr, "ERROR, no port provided\n");
exit(1);
}

sockfd = socket(AF_INET, SOCK_STREAM, 0);


if (sockfd < 0) error("ERROR opening socket");

bzero((char *) &serv_addr, sizeof(serv_addr));


portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);

if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)


error("ERROR on binding");

listen(sockfd, 5);
clilen = sizeof(cli_addr);
printf("SERVER: Waiting for client...\n");
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) error("ERROR on accept");

bzero(buffer, 256);
if (read(newsockfd, buffer, 255) < 0)
error("ERROR reading from socket");

printf("SERVER: %s\n", buffer);


if ((fd = fopen(buffer, "r")) != NULL) {
fread(c, sizeof(char), 2000, fd);
fclose(fd);
printf("SERVER: %s found! Transferring contents...\n", buffer);
if (write(newsockfd, c, strlen(c)) < 0)
error("ERROR writing to socket");
} else {
printf("SERVER: File not found!\n");
if (write(newsockfd, "File not found!", 15) < 0)
error("ERROR writing to socket");
}

close(newsockfd);
close(sockfd);
return 0;
}

CLIENT:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

void error(const char *msg) {


perror(msg);
exit(0);
}

int main(int argc, char *argv[]) {


int sockfd, portno;
struct sockaddr_in serv_addr;
struct hostent *server;
char filepath[256], buf[3000];

if (argc < 3) {
fprintf(stderr, "usage %s hostname port\n", argv[0]);
exit(0);
}

portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) error("ERROR opening socket");

server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr, "ERROR, no such host\n");
exit(0);
}

bzero((char *) &serv_addr, sizeof(serv_addr));


serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);

if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)


error("ERROR connecting");

printf("Client: Enter path with filename: ");


scanf("%s", filepath);
if (write(sockfd, filepath, strlen(filepath)) < 0)
error("ERROR writing to socket");

bzero(buf, 3000);
if (read(sockfd, buf, 2999) < 0)
error("ERROR reading from socket");

printf("Client: Displaying from socket:\n%s\n", buf);

close(sockfd);
return 0;
}

5.
#include <stdio.h>

int h[12];

void genhamcode() {
h[1] = (h[3] + h[5] + h[7] + h[9] + h[11]) % 2;
h[2] = (h[3] + h[6] + h[7] + h[10] + h[11]) % 2;
h[4] = (h[5] + h[6] + h[7]) % 2;
h[8] = (h[9] + h[10] + h[11]) % 2;
printf("\nTransmitted codeword: ");
for (int i = 1; i < 12; i++) printf(" %d", h[i]);
printf("\n");
}

void makeerror() {
int pos;
printf("\nEnter the position to make error: ");
scanf("%d", &pos);
h[pos] ^= 1; // Flip the bit
printf("\nError codeword: ");
for (int i = 1; i < 12; i++) printf(" %d", h[i]);
printf("\n");
}

void correcterror() {
int errpos = (h[1] + h[3] + h[5] + h[7] + h[9] + h[11]) % 2 +
((h[2] + h[3] + h[6] + h[7] + h[10] + h[11]) % 2) * 2 +
((h[4] + h[5] + h[6] + h[7]) % 2) * 4 +
((h[8] + h[9] + h[10] + h[11]) % 2) * 8;

printf("\nError detected at position %d", errpos);


if (errpos) h[errpos] ^= 1; // Correct the error
printf("\nCorrected codeword: ");
for (int i = 1; i < 12; i++) printf(" %d", h[i]);
printf("\n");
}

int main() {
printf("\nEnter the message bits: ");
for (int i = 1; i < 12; i++)
if (i == 3 || i == 5 || i == 6 || i == 7 || i == 9 || i == 10 || i == 11)
scanf("%d", &h[i]);

printf("Message: ");
for (int i = 1; i < 12; i++) printf("%d", h[i]);
printf("\n");

genhamcode();

int ch;
printf("\nDo you want to make an error? (0 or 1): ");
scanf("%d", &ch);
if (ch) {
makeerror();
correcterror();
} else {
printf("\nNo error.\n");
}

return 0;
}

6.

SENDER:
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
int sock, connected;
char fr[30];
struct sockaddr_in server_addr, client_addr;
socklen_t sin_size;

sock = socket(AF_INET, SOCK_STREAM, 0);


if (sock == -1) {
perror("Socket creation failed");
exit(1);
}

int opt = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

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

if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {


perror("Bind failed");
close(sock);
exit(1);
}

listen(sock, 5);
sin_size = sizeof(client_addr);
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
if (connected == -1) {
perror("Accept failed");
close(sock);
exit(1);
}

int i = 1;
while (1) {
printf("Enter Data Frame %d (Enter 'exit' to end): ", i);
scanf("%s", fr);
send(connected, fr, strlen(fr), 0);
if (strcmp(fr, "exit") == 0) break;
recv(sock, fr, sizeof(fr), 0);
printf("Acknowledgment: %s\n", fr);
i++;
}

close(sock);
return 0;
}

RECEIVER:

#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
int sock;
char receive[30];
struct hostent *host;
struct sockaddr_in server_addr;

host = gethostbyname("127.0.0.1");
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(17000);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
memset(&(server_addr.sin_zero), 0, 8);

if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {


perror("Connect failed");
close(sock);
exit(1);
}

int i = 1;
while (1) {
int bytes_received = recv(sock, receive, sizeof(receive) - 1, 0);
receive[bytes_received] = '\0';
if (strcmp(receive, "exit") == 0) break;
printf("\nFrame %d data %s received\n", i, receive);
send(sock, (strlen(receive) < 10) ? receive : "negative", (strlen(receive) < 10) ?
strlen(receive) : 8, 0);
i++;
}

close(sock);
return 0;
}

7.

SERVER:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>

#define FIFO1 "fifo1"


#define FIFO2 "fifo2"

int main() {
char p[100], c[300], ch;
int fd, fd2, f1, num, i = 0;

mkfifo(FIFO1, 0666);
mkfifo(FIFO2, 0666);
printf("SERVER ONLINE\n");

fd = open(FIFO1, O_RDONLY);
printf("Client online\nWaiting for request...\n");

num = read(fd, p, sizeof(p) - 1);


if (num > 0) {
p[num] = '\0';
if ((f1 = open(p, O_RDONLY)) < 0) {
printf("Server: %s not found\n", p);
} else {
printf("Server: %s found, transferring contents...\n", p);
FILE *fp = fdopen(f1, "r");
while ((ch = fgetc(fp)) != EOF)
c[i++] = ch;
c[i] = '\0';
fclose(fp);

fd2 = open(FIFO2, O_WRONLY);


write(fd2, c, strlen(c));
close(fd2);
printf("Server: Transfer completed\n");
}
} else {
perror("Read error");
}

close(fd);
unlink(FIFO1);
unlink(FIFO2);
return 0;
}
RECEIVER:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>

#define FIFO1 "fifo1"


#define FIFO2 "fifo2"

int main() {
char p[100], c[3000];
int fd, fd2, num;

mkfifo(FIFO1, 0666);
mkfifo(FIFO2, 0666);
printf("Waiting for server...\n");

fd = open(FIFO1, O_WRONLY);
printf("SERVER ONLINE!\nCLIENT: Enter the path\n");

while (fgets(p, sizeof(p), stdin)) {


p[strcspn(p, "\n")] = 0; // Remove newline character
if (write(fd, p, strlen(p)) == -1) {
perror("Write error");
} else {
printf("Waiting for reply...\n");
fd2 = open(FIFO2, O_RDONLY);
if ((num = read(fd2, c, sizeof(c) - 1)) > 0) {
c[num] = '\0';
printf("File received! Displaying the contents:\n%s", c);
close(fd2);
break;
} else {
perror("Transfer error");
}
}
}

close(fd);
unlink(FIFO1);
unlink(FIFO2);
return 0;
}

8.
SERVER:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void error(char *msg) {


perror(msg);
exit(0);
}

int main(int argc, char *argv[]) {


int sock, n;
struct sockaddr_in server, from;
socklen_t fromlen;
char buf[1024];

if (argc < 2) {
fprintf(stderr, "ERROR, no port provided\n");
exit(0);
}

sock = socket(AF_INET, SOCK_DGRAM, 0);


if (sock < 0) error("Opening socket");

memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(atoi(argv[1]));

if (bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0)


error("Binding");

fromlen = sizeof(struct sockaddr_in);


while (1) {
n = recvfrom(sock, buf, 1024, 0, (struct sockaddr *)&from, &fromlen);
if (n < 0) error("recvfrom");
write(1, "Received a datagram: ", 21);
write(1, buf, n);
sendto(sock, "Got your message\n", 17, 0, (struct sockaddr *)&from, fromlen);
}

return 0;
}

CLIENT:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void error(char *msg) {


perror(msg);
exit(0);
}

int main(int argc, char *argv[]) {


int sock, n;
struct sockaddr_in server;
struct hostent *hp;
char buffer[256];

if (argc != 3) {
printf("Usage: server port\n");
exit(1);
}

sock = socket(AF_INET, SOCK_DGRAM, 0);


if (sock < 0) error("socket");

server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
if (hp == 0) error("Unknown host");

memcpy((char *)&server.sin_addr, hp->h_addr, hp->h_length);


server.sin_port = htons(atoi(argv[2]));

printf("Please enter the message: ");


fgets(buffer, sizeof(buffer), stdin);

n = sendto(sock, buffer, strlen(buffer), 0, (struct sockaddr *)&server, sizeof(server));


if (n < 0) error("Sendto");

n = recvfrom(sock, buffer, sizeof(buffer), 0, NULL, NULL);


if (n < 0) error("recvfrom");

write(1, "Got an ack: ", 12);


write(1, buffer, n);
return 0;
}

11.

#include <stdio.h>
#include <stdlib.h>

#define min(x,y) ((x) < (y) ? (x) : (y))


#define MAX 25

int main() {
int cap, oprt, cont = 0, inp[MAX], ch, nsec = 0, drop = 0;

printf("LEAKY BUCKET ALGORITHM\n");


printf("Enter the bucket size: ");
scanf("%d", &cap);
printf("Enter the output rate: ");
scanf("%d", &oprt);

while (1) {
printf("Enter the number of packets entering at %d seconds: ", nsec + 1);
scanf("%d", &inp[nsec++]);
printf("Enter 1 to insert packet or 0 to quit: ");
scanf("%d", &ch);
if (!ch) break;
}

printf("\n(SECOND):(PACKET RECEIVED):(PACKET SENT):(PACKET LEFT):(PACKET


DROPPED)\n");
for (int i = 0; i < nsec; i++) {
cont += inp[i];
if (cont > cap) {
drop = cont - cap;
cont = cap;
}
printf("(%d): \t\t(%d): \t\t(%d): \t\t(%d): \t\t(%d)\n", i + 1, inp[i], min(cont, oprt), cont -
min(cont, oprt), drop);
cont -= min(cont, oprt);
}

for (int i = nsec; cont > 0; i++) {


drop = 0;
printf("(%d): \t\t(0): \t\t(%d): \t\t(%d): \t\t(%d)\n", i + 1, min(cont, oprt), cont - min(cont,
oprt), drop);
cont -= min(cont, oprt);
}
return 0;
}

12.

SERVER:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

int main() {
struct sockaddr_in server, client;
int s, n;
char b1[100], b2[100];
struct hostent *hen;
char *IPaddr;

s = socket(AF_INET, SOCK_DGRAM, 0);


if (s < 0) { perror("Socket error"); exit(1); }

server.sin_family = AF_INET;
server.sin_port = htons(5000);
server.sin_addr.s_addr = INADDR_ANY;

if (bind(s, (struct sockaddr *)&server, sizeof(server)) < 0) {


perror("Bind error"); exit(1);
}

n = sizeof(client);
printf("DNS is ready...\n");

while (1) {
recvfrom(s, b1, sizeof(b1), 0, (struct sockaddr *)&client, &n);
hen = gethostbyname(b1);

if (hen) {
IPaddr = inet_ntoa(*((struct in_addr *)hen->h_addr));
strcpy(b2, IPaddr);
sendto(s, b2, sizeof(b2), 0, (struct sockaddr *)&client, n);
} else {
strcpy(b2, "Not found");
sendto(s, b2, sizeof(b2), 0, (struct sockaddr *)&client, n);
}
}
return 0;
}

CLIENT:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

int main() {
struct sockaddr_in server;
int s, n;
char b1[100], b2[100];

s = socket(AF_INET, SOCK_DGRAM, 0);


if (s < 0) { perror("Socket error"); exit(1); }

server.sin_family = AF_INET;
server.sin_port = htons(5000);
server.sin_addr.s_addr = inet_addr("127.0.0.1");

n = sizeof(server);

while (1) {
printf("Enter domain name: ");
scanf("%s", b2);
sendto(s, b2, strlen(b2) + 1, 0, (struct sockaddr *)&server, n);
recvfrom(s, b1, sizeof(b1), 0, NULL, NULL);
printf("DNS IP address: %s\n", b1);
}
return 0;
}

You might also like