diff options
-rw-r--r-- | q6/a/client.c | 47 | ||||
-rw-r--r-- | q6/a/server.c | 49 | ||||
-rw-r--r-- | q6/b/client.c | 47 | ||||
-rw-r--r-- | q6/b/server.c | 44 | ||||
-rw-r--r-- | q6/c/client.c | 41 | ||||
-rw-r--r-- | q6/c/server.c | 38 |
6 files changed, 266 insertions, 0 deletions
diff --git a/q6/a/client.c b/q6/a/client.c new file mode 100644 index 0000000..7219476 --- /dev/null +++ b/q6/a/client.c | |||
@@ -0,0 +1,47 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <sys/types.h> | ||
4 | #include <sys/socket.h> | ||
5 | #include <netinet/in.h> | ||
6 | #include <unistd.h> | ||
7 | #include <string.h> | ||
8 | #include <arpa/inet.h> | ||
9 | #include <string.h> | ||
10 | |||
11 | int main(int argc, char* argv[]) { | ||
12 | const char *server_name = "127.0.0.1"; | ||
13 | int sockfd, connfd; | ||
14 | struct sockaddr_in servaddr, cli; | ||
15 | |||
16 | sockfd = socket(AF_INET, SOCK_STREAM, 0); | ||
17 | servaddr.sin_family = AF_INET; | ||
18 | inet_pton(AF_INET, server_name, &servaddr.sin_addr); | ||
19 | |||
20 | int port = atoi(argv[1]); | ||
21 | servaddr.sin_port = htons(port); | ||
22 | |||
23 | if((connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) == 0) { | ||
24 | printf("Connected to server!\n"); | ||
25 | fflush(stdout); | ||
26 | } else { | ||
27 | printf("Invalid or busy server address\n"); | ||
28 | fflush(stdout); | ||
29 | exit(0); | ||
30 | }; | ||
31 | |||
32 | int cont = 0; | ||
33 | char *buf = malloc(1024); | ||
34 | printf("Client input: "); | ||
35 | fgets(buf, 100, stdin); | ||
36 | while(strcmp(buf, "quit\n") != 0) { | ||
37 | send(sockfd, buf, 100, 0); | ||
38 | if ((cont = recv(sockfd, buf, 100, 0)) > 0) { | ||
39 | printf("Server response: %s\n", buf);; | ||
40 | fflush(stdout); | ||
41 | } | ||
42 | printf("Client input: "); | ||
43 | fgets(buf, 100, stdin); | ||
44 | }; | ||
45 | |||
46 | close(sockfd); | ||
47 | } | ||
diff --git a/q6/a/server.c b/q6/a/server.c new file mode 100644 index 0000000..ac258c0 --- /dev/null +++ b/q6/a/server.c | |||
@@ -0,0 +1,49 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <sys/types.h> | ||
4 | #include <sys/socket.h> | ||
5 | #include <netinet/in.h> | ||
6 | #include <unistd.h> | ||
7 | #include <string.h> | ||
8 | #include <arpa/inet.h> | ||
9 | |||
10 | int main(int argc, char *argv[]) { | ||
11 | int sockfd, connfd; | ||
12 | struct sockaddr_in servaddr, cli; | ||
13 | |||
14 | sockfd = socket(AF_INET, SOCK_STREAM, 0); | ||
15 | servaddr.sin_family = AF_INET; | ||
16 | servaddr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
17 | |||
18 | int port = atoi(argv[1]); | ||
19 | servaddr.sin_port = htons(port); | ||
20 | |||
21 | bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); | ||
22 | |||
23 | listen(sockfd, 128); | ||
24 | printf("Server started at %s...\n", inet_ntoa(servaddr.sin_addr)); | ||
25 | fflush(stdout); | ||
26 | |||
27 | while(1) { | ||
28 | socklen_t len = sizeof(cli); | ||
29 | connfd = accept(sockfd, (struct sockaddr *)&cli, &len); | ||
30 | int i = getpeername(connfd, (struct sockaddr*)&cli, &len); | ||
31 | |||
32 | printf("Client connected at port %d\n", htons(cli.sin_port)); | ||
33 | int n; | ||
34 | char buf[100]; | ||
35 | |||
36 | int pid; | ||
37 | if((pid=fork())==0){ | ||
38 | close(sockfd); | ||
39 | while((n = recv(connfd, buf, 100, 0)) > 0) { | ||
40 | printf("Client message: %s\n", buf); | ||
41 | fflush(stdout); | ||
42 | send(connfd, buf, n, 0); | ||
43 | }; | ||
44 | exit(0); | ||
45 | } | ||
46 | close(connfd); | ||
47 | } | ||
48 | return 0; | ||
49 | } | ||
diff --git a/q6/b/client.c b/q6/b/client.c new file mode 100644 index 0000000..7219476 --- /dev/null +++ b/q6/b/client.c | |||
@@ -0,0 +1,47 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <sys/types.h> | ||
4 | #include <sys/socket.h> | ||
5 | #include <netinet/in.h> | ||
6 | #include <unistd.h> | ||
7 | #include <string.h> | ||
8 | #include <arpa/inet.h> | ||
9 | #include <string.h> | ||
10 | |||
11 | int main(int argc, char* argv[]) { | ||
12 | const char *server_name = "127.0.0.1"; | ||
13 | int sockfd, connfd; | ||
14 | struct sockaddr_in servaddr, cli; | ||
15 | |||
16 | sockfd = socket(AF_INET, SOCK_STREAM, 0); | ||
17 | servaddr.sin_family = AF_INET; | ||
18 | inet_pton(AF_INET, server_name, &servaddr.sin_addr); | ||
19 | |||
20 | int port = atoi(argv[1]); | ||
21 | servaddr.sin_port = htons(port); | ||
22 | |||
23 | if((connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) == 0) { | ||
24 | printf("Connected to server!\n"); | ||
25 | fflush(stdout); | ||
26 | } else { | ||
27 | printf("Invalid or busy server address\n"); | ||
28 | fflush(stdout); | ||
29 | exit(0); | ||
30 | }; | ||
31 | |||
32 | int cont = 0; | ||
33 | char *buf = malloc(1024); | ||
34 | printf("Client input: "); | ||
35 | fgets(buf, 100, stdin); | ||
36 | while(strcmp(buf, "quit\n") != 0) { | ||
37 | send(sockfd, buf, 100, 0); | ||
38 | if ((cont = recv(sockfd, buf, 100, 0)) > 0) { | ||
39 | printf("Server response: %s\n", buf);; | ||
40 | fflush(stdout); | ||
41 | } | ||
42 | printf("Client input: "); | ||
43 | fgets(buf, 100, stdin); | ||
44 | }; | ||
45 | |||
46 | close(sockfd); | ||
47 | } | ||
diff --git a/q6/b/server.c b/q6/b/server.c new file mode 100644 index 0000000..e217c2b --- /dev/null +++ b/q6/b/server.c | |||
@@ -0,0 +1,44 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <sys/types.h> | ||
4 | #include <sys/socket.h> | ||
5 | #include <netinet/in.h> | ||
6 | #include <unistd.h> | ||
7 | #include <string.h> | ||
8 | #include <arpa/inet.h> | ||
9 | |||
10 | int main(int argc, char *argv[]) { | ||
11 | int sockfd, connfd; | ||
12 | struct sockaddr_in servaddr, cli; | ||
13 | |||
14 | sockfd = socket(AF_INET, SOCK_STREAM, 0); | ||
15 | servaddr.sin_family = AF_INET; | ||
16 | servaddr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
17 | |||
18 | int port = atoi(argv[1]); | ||
19 | servaddr.sin_port = htons(port); | ||
20 | |||
21 | bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); | ||
22 | |||
23 | listen(sockfd, 128); | ||
24 | printf("Server started at %s...\n", inet_ntoa(servaddr.sin_addr)); | ||
25 | fflush(stdout); | ||
26 | |||
27 | while(1) { | ||
28 | socklen_t len = sizeof(cli); | ||
29 | connfd = accept(sockfd, (struct sockaddr *)&cli, &len); | ||
30 | int i = getpeername(connfd, (struct sockaddr*)&cli, &len); | ||
31 | |||
32 | printf("Client connected at port %d\n", htons(cli.sin_port)); | ||
33 | int n; | ||
34 | char buf[100]; | ||
35 | |||
36 | while((n = recv(connfd, buf, 100, 0)) > 0) { | ||
37 | printf("Client message: %s\n", buf); | ||
38 | fflush(stdout); | ||
39 | send(connfd, buf, n, 0); | ||
40 | }; | ||
41 | close(connfd); | ||
42 | } | ||
43 | return 0; | ||
44 | } | ||
diff --git a/q6/c/client.c b/q6/c/client.c new file mode 100644 index 0000000..12e00fa --- /dev/null +++ b/q6/c/client.c | |||
@@ -0,0 +1,41 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <string.h> | ||
4 | #include <unistd.h> | ||
5 | #include <sys/types.h> | ||
6 | #include <sys/socket.h> | ||
7 | #include <arpa/inet.h> | ||
8 | #include <netinet/in.h> | ||
9 | #include <string.h> | ||
10 | |||
11 | int main(int argc, char *argv[]) { | ||
12 | int sockfd; | ||
13 | struct sockaddr_in servaddr; | ||
14 | sockfd = socket(AF_INET, SOCK_DGRAM, 0); | ||
15 | |||
16 | servaddr.sin_family = AF_INET; | ||
17 | servaddr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
18 | |||
19 | int port = atoi(argv[1]); | ||
20 | servaddr.sin_port = htons(port); | ||
21 | |||
22 | int n; | ||
23 | |||
24 | socklen_t len = sizeof(servaddr); | ||
25 | char *buf = malloc(100); | ||
26 | fgets(buf, 100, stdin); | ||
27 | while(strcmp(buf, "quit\n") != 0) { | ||
28 | sendto(sockfd, buf, strlen(buf), | ||
29 | MSG_CONFIRM, (const struct sockaddr *) &servaddr, | ||
30 | sizeof(servaddr)); | ||
31 | |||
32 | n = recvfrom(sockfd, buf, 100, | ||
33 | MSG_WAITALL, (struct sockaddr *) &servaddr, | ||
34 | &len); | ||
35 | printf("Recieved: %s", buf); | ||
36 | fgets(buf, 100, stdin); | ||
37 | } | ||
38 | |||
39 | close(sockfd); | ||
40 | return 0; | ||
41 | } | ||
diff --git a/q6/c/server.c b/q6/c/server.c new file mode 100644 index 0000000..4b25070 --- /dev/null +++ b/q6/c/server.c | |||
@@ -0,0 +1,38 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <string.h> | ||
4 | #include <unistd.h> | ||
5 | #include <sys/types.h> | ||
6 | #include <sys/socket.h> | ||
7 | #include <arpa/inet.h> | ||
8 | #include <netinet/in.h> | ||
9 | |||
10 | int main(int argc, char *argv[]) { | ||
11 | int sockfd; | ||
12 | struct sockaddr_in servaddr, cli; | ||
13 | sockfd = socket(AF_INET, SOCK_DGRAM, 0); | ||
14 | |||
15 | servaddr.sin_family = AF_INET; | ||
16 | servaddr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
17 | |||
18 | int port = atoi(argv[1]); | ||
19 | servaddr.sin_port = htons(port); | ||
20 | |||
21 | bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); | ||
22 | |||
23 | socklen_t len = sizeof(cli); | ||
24 | while(1) { | ||
25 | int n; | ||
26 | char *buf = malloc(100); | ||
27 | while( | ||
28 | (n = recvfrom(sockfd, (char *)buf, 100, MSG_WAITALL, (struct sockaddr*) &cli, &len)) | ||
29 | > 0 | ||
30 | ) { | ||
31 | printf("Client message: %s\n", buf); | ||
32 | fflush(stdout); | ||
33 | sendto(sockfd, buf, n, MSG_CONFIRM, (const struct sockaddr *) &cli, len); | ||
34 | } | ||
35 | } | ||
36 | close(sockfd); | ||
37 | return 0; | ||
38 | } | ||