From e8186e657d51d9deedd171981d314933ed408db5 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 16 Dec 2020 16:05:10 +0530 Subject: add q3, q2, images --- q2/ford.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ q3/checksum.c | 37 ++++++++++++++++++++++++++++ q3/checksum.o | Bin 0 -> 18600 bytes q3/hamming.c | 56 +++++++++++++++++++++++++++++++++++++++++ q3/hamming.o | Bin 0 -> 18568 bytes readme.txt | 26 +++++++++++++++++--- 6 files changed, 194 insertions(+), 3 deletions(-) create mode 100644 q2/ford.c create mode 100644 q3/checksum.c create mode 100755 q3/checksum.o create mode 100644 q3/hamming.c create mode 100755 q3/hamming.o diff --git a/q2/ford.c b/q2/ford.c new file mode 100644 index 0000000..993a0c3 --- /dev/null +++ b/q2/ford.c @@ -0,0 +1,78 @@ +#include + +int A[10][10], n, d[10], p[10]; + +int bellman_ford() { + int i, u, v; + for (int i = 0; i < n; i++) { + for (u = 0; u < n; u++) { + for (v = 0; v < n; v++) { + if (d[v] > d[u] + A[u][v]) { + d[v] = d[u] + A[u][v]; + p[v] = u; + } + } + } + } + for (u = 0; u < n; u++) { + for (v = 0; v < n; v++) { + if (d[v] > d[u] + A[u][v]) { + printf("Detected negative edge\n"); + return -1; + } + } + } + return 0; +} + +int main() { + scanf("%d", &n); + int source = 0, destination = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + scanf("%d", &A[i][j]); + } + } + scanf("%d", &source); + scanf("%d", &destination); + + printf("Input graph:\n"); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + printf("%3d ", A[i][j]); + } + printf("\n"); + } + + for (int i = 0; i < n; i++) { + d[i] = 999; + p[i] = -1; + } + d[source] = 0; + int valid = bellman_ford(); + if (valid == -1) { + printf("Graph contains negative edge\n"); + return 0; + } + printf("\nFrom Router %d to Router %d\n", source, destination); + // for(int i=0; i +#include + +int checksum() { + unsigned int parts[9]; + unsigned int sum; + printf("Enter the parts, one per line:\n"); + for (int i = 0; i < 9; i++) { + scanf("%X", &parts[i]); + sum += parts[i]; + while (sum >> 16) + sum = (sum & 0xffff) + (sum >> 16); + } + return ~sum; +} + +int main() { + printf("--- SENDER ---\n"); + int sender_checksum = checksum(); + printf("\n--- RECV ---\n"); + int recv_checksum = checksum(); + printf("Sent checksum: %x\n", sender_checksum); + printf("Recieved checksum: %x\n", recv_checksum); + if (sender_checksum == recv_checksum) { + printf("Matching checksums!\n"); + return 0; + } else { + printf("Mismatched checksums!\n"); + return 0; + } +} + +// 00004500 00000073 00000000 00004000 00004011 0000c0a8 00000001 0000c0a8 000000c7 +// checksum - ffffb861 + +// 00004500 0000003c 00001c46 00004000 00004006 0000ac10 00000a63 0000ac10 00000a0c +// checksum - 0000b1e6 diff --git a/q3/checksum.o b/q3/checksum.o new file mode 100755 index 0000000..cfcaed6 Binary files /dev/null and b/q3/checksum.o differ diff --git a/q3/hamming.c b/q3/hamming.c new file mode 100644 index 0000000..01d0f0e --- /dev/null +++ b/q3/hamming.c @@ -0,0 +1,56 @@ +#include + +void main() { + int data[10]; + int dataatrec[10],c,c1,c2,c3,i; + + printf("Sender: "); + scanf("%d %d %d %d",&data[0] ,&data[1] ,&data[2] ,&data[4]); + + //Calculation of even parity + data[6]=data[0]^data[2]^data[4]; + data[5]=data[0]^data[1]^data[4]; + data[3]=data[0]^data[1]^data[2]; + + printf("Encoded Data: "); + for(i=0;i<7;i++) + printf("%d",data[i]); + + printf("\nReceiver: "); + for(i=0;i<7;i++) + scanf("%d",&dataatrec[i]); + + c1=dataatrec[6]^dataatrec[4]^dataatrec[2]^dataatrec[0]; + c2=dataatrec[5]^dataatrec[4]^dataatrec[1]^dataatrec[0]; + c3=dataatrec[3]^dataatrec[2]^dataatrec[1]^dataatrec[0]; + c=c3*4+c2*2+c1 ; + + printf("\nSyndrome bits: %d %d %d", c1, c2, c3); + + if(c==0) { + printf("\nNo error while transmission of data\n"); + } + else { + printf("\nError Position: %d",c); + + printf("\nData sent: "); + for(i=0;i<7;i++) + printf("%d",data[i]); + + printf("\nData received: "); + for(i=0;i<7;i++) + printf("%d",dataatrec[i]); + + printf("\nCorrect message is: "); + + //if errorneous bit is 0 we complement it else vice versa + if(dataatrec[7-c]==0) + dataatrec[7-c]=1; + else + dataatrec[7-c]=0; + + for (i=0;i<7;i++) { + printf("%d",dataatrec[i]); + } + } +} diff --git a/q3/hamming.o b/q3/hamming.o new file mode 100755 index 0000000..fa9cb70 Binary files /dev/null and b/q3/hamming.o differ diff --git a/readme.txt b/readme.txt index 6aabaaf..5caeacf 100644 --- a/readme.txt +++ b/readme.txt @@ -1,5 +1,7 @@ 18CS54 - network programming and security ------------------------------------------------------------- +============================================================ + + Q1: Write a networking program consisting of a client and server components. The client must request for a file by providing a file name, and the server must respond with the @@ -9,8 +11,26 @@ Q1: Write a networking program consisting of a client and A1: https://u.peppe.rs/qt.png +------------------------------------------------------------ + +Q2: Write a program to illustrate the function of + Distance-Vector routing using Bellman-Ford algorithm. Handle + the situation where negative edges are present. + +A2: https://u.peppe.rs/LS.png + https://u.peppe.rs/ep.png + +------------------------------------------------------------ + +Q3: Write a program to showcase + a) Checksum - error detection + b) Hamming code - error detection and correction + +A3: https://u.peppe.rs/qQ.png + https://u.peppe.rs/qr.png ------------------------------------------------------------ + Q6: Write a networking program to demonstrate the following: a) Concurrent TCP server b) Iterative TCP server @@ -20,8 +40,8 @@ A6: a) https://u.peppe.rs/E3.png b) https://u.peppe.rs/VA.png c) https://u.peppe.rs/ei.png - ------------------------------------------------------------ + Q7: Write a networking program that recieves a shell command from a client and returns the output of the command to the client. @@ -29,5 +49,5 @@ Q7: Write a networking program that recieves a shell command A7: https://u.peppe.rs/QD.png ------------------------------------------------------------- +============================================================ mirror of git.peppe.rs/university/nps -- cgit v1.2.3