diff options
author | Akshay <[email protected]> | 2020-12-09 09:01:31 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-12-09 09:01:31 +0000 |
commit | a33f3a8a5a392ae7bcc8187216cff8583b7518ac (patch) | |
tree | 5b78a449382e0b4cc7d20e77315fad8a4bb4a229 /q5 | |
parent | 5664bba1f343fb2f987d136527ffc742afc76f7e (diff) |
add question 5
Diffstat (limited to 'q5')
-rw-r--r-- | q5/client.c | 47 | ||||
-rw-r--r-- | q5/server.c | 86 | ||||
-rw-r--r-- | q5/with-threads/client.c | 47 | ||||
-rw-r--r-- | q5/with-threads/server.c | 72 |
4 files changed, 252 insertions, 0 deletions
diff --git a/q5/client.c b/q5/client.c new file mode 100644 index 0000000..7219476 --- /dev/null +++ b/q5/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/q5/server.c b/q5/server.c new file mode 100644 index 0000000..87870bb --- /dev/null +++ b/q5/server.c | |||
@@ -0,0 +1,86 @@ | |||
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 <pthread.h> | ||
10 | |||
11 | void* handle_client(void *); | ||
12 | |||
13 | int client_list[16]; | ||
14 | int client_list_len; | ||
15 | |||
16 | int main(int argc, char *argv[]) { | ||
17 | int sockfd, connfd; | ||
18 | |||
19 | pthread_mutex_t list_lock; | ||
20 | pthread_mutex_init(&list_lock, NULL); | ||
21 | |||
22 | struct sockaddr_in servaddr, cli; | ||
23 | |||
24 | sockfd = socket(AF_INET, SOCK_STREAM, 0); | ||
25 | servaddr.sin_family = AF_INET; | ||
26 | servaddr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
27 | |||
28 | int port = atoi(argv[1]); | ||
29 | servaddr.sin_port = htons(port); | ||
30 | |||
31 | bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); | ||
32 | |||
33 | listen(sockfd, 128); | ||
34 | printf("Server started at %s...\n", inet_ntoa(servaddr.sin_addr)); | ||
35 | fflush(stdout); | ||
36 | |||
37 | while(1) { | ||
38 | socklen_t len = sizeof(cli); | ||
39 | connfd = accept(sockfd, (struct sockaddr *)&cli, &len); | ||
40 | int i = getpeername(connfd, (struct sockaddr*)&cli, &len); | ||
41 | |||
42 | pthread_mutex_lock(&list_lock); | ||
43 | client_list[client_list_len] = connfd; | ||
44 | client_list_len += 1; | ||
45 | pthread_mutex_unlock(&list_lock); | ||
46 | |||
47 | int n; | ||
48 | char buf[100]; | ||
49 | |||
50 | // if((pid=fork())==0){ | ||
51 | // close(sockfd); | ||
52 | // while((n = recv(connfd, buf, 100, 0)) > 0) { | ||
53 | // printf("Client message: %s\n", buf); | ||
54 | // fflush(stdout); | ||
55 | // for (int i = 0; i < client_list_len; i++) { | ||
56 | // int curr_conn = client_list[i]; | ||
57 | // if (i != connfd) { | ||
58 | // send(i, buf, n, 0); | ||
59 | // } | ||
60 | // } | ||
61 | // }; | ||
62 | // exit(0); | ||
63 | // } | ||
64 | |||
65 | pthread_t client_thread; | ||
66 | pthread_create(&client_thread, NULL, &handle_client, &connfd); | ||
67 | } | ||
68 | return 0; | ||
69 | } | ||
70 | |||
71 | void* handle_client(void* param) { | ||
72 | int *connfd = (int *) param; | ||
73 | int n; | ||
74 | char buf[100]; | ||
75 | while ((n = recv(*connfd, buf, 100, 0)) > 0) { | ||
76 | printf("Client message (%d): %s", *connfd, buf); | ||
77 | fflush(stdout); | ||
78 | for (int i = 0; i < client_list_len; i++) { | ||
79 | int curr_conn = *(client_list + i); | ||
80 | if (curr_conn != *connfd) { | ||
81 | send(curr_conn, buf, n, 0); | ||
82 | } | ||
83 | } | ||
84 | } | ||
85 | return 0; | ||
86 | } | ||
diff --git a/q5/with-threads/client.c b/q5/with-threads/client.c new file mode 100644 index 0000000..7219476 --- /dev/null +++ b/q5/with-threads/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/q5/with-threads/server.c b/q5/with-threads/server.c new file mode 100644 index 0000000..c2d5c56 --- /dev/null +++ b/q5/with-threads/server.c | |||
@@ -0,0 +1,72 @@ | |||
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 <pthread.h> | ||
10 | |||
11 | void* handle_client(void *); | ||
12 | |||
13 | int client_list[16]; | ||
14 | int client_list_len; | ||
15 | |||
16 | int main(int argc, char *argv[]) { | ||
17 | int sockfd; | ||
18 | |||
19 | pthread_mutex_t list_lock; | ||
20 | pthread_mutex_init(&list_lock, NULL); | ||
21 | |||
22 | struct sockaddr_in servaddr, cli; | ||
23 | |||
24 | sockfd = socket(AF_INET, SOCK_STREAM, 0); | ||
25 | servaddr.sin_family = AF_INET; | ||
26 | servaddr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
27 | |||
28 | int port = atoi(argv[1]); | ||
29 | servaddr.sin_port = htons(port); | ||
30 | |||
31 | bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); | ||
32 | |||
33 | listen(sockfd, 128); | ||
34 | printf("Server started at %s...\n", inet_ntoa(servaddr.sin_addr)); | ||
35 | fflush(stdout); | ||
36 | |||
37 | while(1) { | ||
38 | socklen_t len = sizeof(cli); | ||
39 | int connfd = accept(sockfd, (struct sockaddr*)&cli, &len); | ||
40 | int i = getpeername(connfd, (struct sockaddr*)&cli, &len); | ||
41 | |||
42 | client_list[client_list_len] = connfd; | ||
43 | client_list_len += 1; | ||
44 | |||
45 | printf("Clients connected: %d \nDescriptors: ", client_list_len); | ||
46 | for(int i = 0; i < client_list_len; i++) { | ||
47 | printf("%d ", client_list[i]); | ||
48 | } | ||
49 | printf("\n"); | ||
50 | fflush(stdout); | ||
51 | |||
52 | pthread_t client_thread; | ||
53 | pthread_create(&client_thread, NULL, &handle_client, &connfd); | ||
54 | } | ||
55 | close(sockfd); | ||
56 | return 0; | ||
57 | } | ||
58 | |||
59 | void* handle_client(void* param) { | ||
60 | int *connfd = (int *) param; | ||
61 | int n; | ||
62 | char buf[100]; | ||
63 | bzero(&buf, 100); | ||
64 | while ((n = recv(*connfd, buf, 100, 0)) > 0) { | ||
65 | printf("Client message (%d): %s", *connfd, buf); | ||
66 | fflush(stdout); | ||
67 | send(*connfd, buf, n, 0); | ||
68 | bzero(&buf, 100); | ||
69 | } | ||
70 | close(*connfd); | ||
71 | return 0; | ||
72 | } | ||