diff options
author | Akshay <[email protected]> | 2020-12-16 10:35:10 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-12-16 10:35:10 +0000 |
commit | e8186e657d51d9deedd171981d314933ed408db5 (patch) | |
tree | 786837fcbd271abf21443652986888ba6120ca63 | |
parent | a33f3a8a5a392ae7bcc8187216cff8583b7518ac (diff) |
add q3, q2, images
-rw-r--r-- | q2/ford.c | 78 | ||||
-rw-r--r-- | q3/checksum.c | 37 | ||||
-rwxr-xr-x | q3/checksum.o | bin | 0 -> 18600 bytes | |||
-rw-r--r-- | q3/hamming.c | 56 | ||||
-rwxr-xr-x | q3/hamming.o | bin | 0 -> 18568 bytes | |||
-rw-r--r-- | readme.txt | 26 |
6 files changed, 194 insertions, 3 deletions
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 @@ | |||
1 | #include <stdio.h> | ||
2 | |||
3 | int A[10][10], n, d[10], p[10]; | ||
4 | |||
5 | int bellman_ford() { | ||
6 | int i, u, v; | ||
7 | for (int i = 0; i < n; i++) { | ||
8 | for (u = 0; u < n; u++) { | ||
9 | for (v = 0; v < n; v++) { | ||
10 | if (d[v] > d[u] + A[u][v]) { | ||
11 | d[v] = d[u] + A[u][v]; | ||
12 | p[v] = u; | ||
13 | } | ||
14 | } | ||
15 | } | ||
16 | } | ||
17 | for (u = 0; u < n; u++) { | ||
18 | for (v = 0; v < n; v++) { | ||
19 | if (d[v] > d[u] + A[u][v]) { | ||
20 | printf("Detected negative edge\n"); | ||
21 | return -1; | ||
22 | } | ||
23 | } | ||
24 | } | ||
25 | return 0; | ||
26 | } | ||
27 | |||
28 | int main() { | ||
29 | scanf("%d", &n); | ||
30 | int source = 0, destination = 0; | ||
31 | for (int i = 0; i < n; i++) { | ||
32 | for (int j = 0; j < n; j++) { | ||
33 | scanf("%d", &A[i][j]); | ||
34 | } | ||
35 | } | ||
36 | scanf("%d", &source); | ||
37 | scanf("%d", &destination); | ||
38 | |||
39 | printf("Input graph:\n"); | ||
40 | for (int i = 0; i < n; i++) { | ||
41 | for (int j = 0; j < n; j++) { | ||
42 | printf("%3d ", A[i][j]); | ||
43 | } | ||
44 | printf("\n"); | ||
45 | } | ||
46 | |||
47 | for (int i = 0; i < n; i++) { | ||
48 | d[i] = 999; | ||
49 | p[i] = -1; | ||
50 | } | ||
51 | d[source] = 0; | ||
52 | int valid = bellman_ford(); | ||
53 | if (valid == -1) { | ||
54 | printf("Graph contains negative edge\n"); | ||
55 | return 0; | ||
56 | } | ||
57 | printf("\nFrom Router %d to Router %d\n", source, destination); | ||
58 | // for(int i=0; i<n; i++){ | ||
59 | // printf("Cost: %2d | Path: ", d[i]); | ||
60 | // if(i != source){ | ||
61 | // int j = i; | ||
62 | // while(p[j] != -1){ | ||
63 | // printf("%d <- ",j); | ||
64 | // j = p[j]; | ||
65 | // } | ||
66 | // } | ||
67 | // printf("%d\n",source); | ||
68 | // } | ||
69 | printf("Cost: %2d | Path: ", d[destination]); | ||
70 | if(destination != source){ | ||
71 | int j = destination; | ||
72 | while(p[j] != -1){ | ||
73 | printf("%d <- ",j); | ||
74 | j = p[j]; | ||
75 | } | ||
76 | } | ||
77 | printf("%d\n",source); | ||
78 | } | ||
diff --git a/q3/checksum.c b/q3/checksum.c new file mode 100644 index 0000000..513ae17 --- /dev/null +++ b/q3/checksum.c | |||
@@ -0,0 +1,37 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdio.h> | ||
3 | |||
4 | int checksum() { | ||
5 | unsigned int parts[9]; | ||
6 | unsigned int sum; | ||
7 | printf("Enter the parts, one per line:\n"); | ||
8 | for (int i = 0; i < 9; i++) { | ||
9 | scanf("%X", &parts[i]); | ||
10 | sum += parts[i]; | ||
11 | while (sum >> 16) | ||
12 | sum = (sum & 0xffff) + (sum >> 16); | ||
13 | } | ||
14 | return ~sum; | ||
15 | } | ||
16 | |||
17 | int main() { | ||
18 | printf("--- SENDER ---\n"); | ||
19 | int sender_checksum = checksum(); | ||
20 | printf("\n--- RECV ---\n"); | ||
21 | int recv_checksum = checksum(); | ||
22 | printf("Sent checksum: %x\n", sender_checksum); | ||
23 | printf("Recieved checksum: %x\n", recv_checksum); | ||
24 | if (sender_checksum == recv_checksum) { | ||
25 | printf("Matching checksums!\n"); | ||
26 | return 0; | ||
27 | } else { | ||
28 | printf("Mismatched checksums!\n"); | ||
29 | return 0; | ||
30 | } | ||
31 | } | ||
32 | |||
33 | // 00004500 00000073 00000000 00004000 00004011 0000c0a8 00000001 0000c0a8 000000c7 | ||
34 | // checksum - ffffb861 | ||
35 | |||
36 | // 00004500 0000003c 00001c46 00004000 00004006 0000ac10 00000a63 0000ac10 00000a0c | ||
37 | // checksum - 0000b1e6 | ||
diff --git a/q3/checksum.o b/q3/checksum.o new file mode 100755 index 0000000..cfcaed6 --- /dev/null +++ b/q3/checksum.o | |||
Binary files 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 @@ | |||
1 | #include<stdio.h> | ||
2 | |||
3 | void main() { | ||
4 | int data[10]; | ||
5 | int dataatrec[10],c,c1,c2,c3,i; | ||
6 | |||
7 | printf("Sender: "); | ||
8 | scanf("%d %d %d %d",&data[0] ,&data[1] ,&data[2] ,&data[4]); | ||
9 | |||
10 | //Calculation of even parity | ||
11 | data[6]=data[0]^data[2]^data[4]; | ||
12 | data[5]=data[0]^data[1]^data[4]; | ||
13 | data[3]=data[0]^data[1]^data[2]; | ||
14 | |||
15 | printf("Encoded Data: "); | ||
16 | for(i=0;i<7;i++) | ||
17 | printf("%d",data[i]); | ||
18 | |||
19 | printf("\nReceiver: "); | ||
20 | for(i=0;i<7;i++) | ||
21 | scanf("%d",&dataatrec[i]); | ||
22 | |||
23 | c1=dataatrec[6]^dataatrec[4]^dataatrec[2]^dataatrec[0]; | ||
24 | c2=dataatrec[5]^dataatrec[4]^dataatrec[1]^dataatrec[0]; | ||
25 | c3=dataatrec[3]^dataatrec[2]^dataatrec[1]^dataatrec[0]; | ||
26 | c=c3*4+c2*2+c1 ; | ||
27 | |||
28 | printf("\nSyndrome bits: %d %d %d", c1, c2, c3); | ||
29 | |||
30 | if(c==0) { | ||
31 | printf("\nNo error while transmission of data\n"); | ||
32 | } | ||
33 | else { | ||
34 | printf("\nError Position: %d",c); | ||
35 | |||
36 | printf("\nData sent: "); | ||
37 | for(i=0;i<7;i++) | ||
38 | printf("%d",data[i]); | ||
39 | |||
40 | printf("\nData received: "); | ||
41 | for(i=0;i<7;i++) | ||
42 | printf("%d",dataatrec[i]); | ||
43 | |||
44 | printf("\nCorrect message is: "); | ||
45 | |||
46 | //if errorneous bit is 0 we complement it else vice versa | ||
47 | if(dataatrec[7-c]==0) | ||
48 | dataatrec[7-c]=1; | ||
49 | else | ||
50 | dataatrec[7-c]=0; | ||
51 | |||
52 | for (i=0;i<7;i++) { | ||
53 | printf("%d",dataatrec[i]); | ||
54 | } | ||
55 | } | ||
56 | } | ||
diff --git a/q3/hamming.o b/q3/hamming.o new file mode 100755 index 0000000..fa9cb70 --- /dev/null +++ b/q3/hamming.o | |||
Binary files differ | |||
@@ -1,5 +1,7 @@ | |||
1 | 18CS54 - network programming and security | 1 | 18CS54 - network programming and security |
2 | ------------------------------------------------------------ | 2 | ============================================================ |
3 | |||
4 | |||
3 | Q1: Write a networking program consisting of a client and | 5 | Q1: Write a networking program consisting of a client and |
4 | server components. The client must request for a file by | 6 | server components. The client must request for a file by |
5 | providing a file name, and the server must respond with the | 7 | 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 | |||
9 | 11 | ||
10 | A1: https://u.peppe.rs/qt.png | 12 | A1: https://u.peppe.rs/qt.png |
11 | 13 | ||
14 | ------------------------------------------------------------ | ||
15 | |||
16 | Q2: Write a program to illustrate the function of | ||
17 | Distance-Vector routing using Bellman-Ford algorithm. Handle | ||
18 | the situation where negative edges are present. | ||
19 | |||
20 | A2: https://u.peppe.rs/LS.png | ||
21 | https://u.peppe.rs/ep.png | ||
22 | |||
23 | ------------------------------------------------------------ | ||
24 | |||
25 | Q3: Write a program to showcase | ||
26 | a) Checksum - error detection | ||
27 | b) Hamming code - error detection and correction | ||
28 | |||
29 | A3: https://u.peppe.rs/qQ.png | ||
30 | https://u.peppe.rs/qr.png | ||
12 | 31 | ||
13 | ------------------------------------------------------------ | 32 | ------------------------------------------------------------ |
33 | |||
14 | Q6: Write a networking program to demonstrate the following: | 34 | Q6: Write a networking program to demonstrate the following: |
15 | a) Concurrent TCP server | 35 | a) Concurrent TCP server |
16 | b) Iterative TCP server | 36 | b) Iterative TCP server |
@@ -20,8 +40,8 @@ A6: a) https://u.peppe.rs/E3.png | |||
20 | b) https://u.peppe.rs/VA.png | 40 | b) https://u.peppe.rs/VA.png |
21 | c) https://u.peppe.rs/ei.png | 41 | c) https://u.peppe.rs/ei.png |
22 | 42 | ||
23 | |||
24 | ------------------------------------------------------------ | 43 | ------------------------------------------------------------ |
44 | |||
25 | Q7: Write a networking program that recieves a shell command | 45 | Q7: Write a networking program that recieves a shell command |
26 | from a client and returns the output of the command to the | 46 | from a client and returns the output of the command to the |
27 | client. | 47 | client. |
@@ -29,5 +49,5 @@ Q7: Write a networking program that recieves a shell command | |||
29 | A7: https://u.peppe.rs/QD.png | 49 | A7: https://u.peppe.rs/QD.png |
30 | 50 | ||
31 | 51 | ||
32 | ------------------------------------------------------------ | 52 | ============================================================ |
33 | mirror of git.peppe.rs/university/nps | 53 | mirror of git.peppe.rs/university/nps |