From a33f3a8a5a392ae7bcc8187216cff8583b7518ac Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 9 Dec 2020 14:31:31 +0530 Subject: add question 5 --- q5/client.c | 47 ++++++++++++++++++++++++++ q5/server.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ q5/with-threads/client.c | 47 ++++++++++++++++++++++++++ q5/with-threads/server.c | 72 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 252 insertions(+) create mode 100644 q5/client.c create mode 100644 q5/server.c create mode 100644 q5/with-threads/client.c create mode 100644 q5/with-threads/server.c (limited to 'q5') 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 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]) { + const char *server_name = "127.0.0.1"; + int sockfd, connfd; + struct sockaddr_in servaddr, cli; + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + servaddr.sin_family = AF_INET; + inet_pton(AF_INET, server_name, &servaddr.sin_addr); + + int port = atoi(argv[1]); + servaddr.sin_port = htons(port); + + if((connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) == 0) { + printf("Connected to server!\n"); + fflush(stdout); + } else { + printf("Invalid or busy server address\n"); + fflush(stdout); + exit(0); + }; + + int cont = 0; + char *buf = malloc(1024); + printf("Client input: "); + fgets(buf, 100, stdin); + while(strcmp(buf, "quit\n") != 0) { + send(sockfd, buf, 100, 0); + if ((cont = recv(sockfd, buf, 100, 0)) > 0) { + printf("Server response: %s\n", buf);; + fflush(stdout); + } + printf("Client input: "); + fgets(buf, 100, stdin); + }; + + close(sockfd); +} 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 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void* handle_client(void *); + +int client_list[16]; +int client_list_len; + +int main(int argc, char *argv[]) { + int sockfd, connfd; + + pthread_mutex_t list_lock; + pthread_mutex_init(&list_lock, NULL); + + struct sockaddr_in servaddr, cli; + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + + int port = atoi(argv[1]); + servaddr.sin_port = htons(port); + + bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); + + listen(sockfd, 128); + printf("Server started at %s...\n", inet_ntoa(servaddr.sin_addr)); + fflush(stdout); + + while(1) { + socklen_t len = sizeof(cli); + connfd = accept(sockfd, (struct sockaddr *)&cli, &len); + int i = getpeername(connfd, (struct sockaddr*)&cli, &len); + + pthread_mutex_lock(&list_lock); + client_list[client_list_len] = connfd; + client_list_len += 1; + pthread_mutex_unlock(&list_lock); + + int n; + char buf[100]; + + // if((pid=fork())==0){ + // close(sockfd); + // while((n = recv(connfd, buf, 100, 0)) > 0) { + // printf("Client message: %s\n", buf); + // fflush(stdout); + // for (int i = 0; i < client_list_len; i++) { + // int curr_conn = client_list[i]; + // if (i != connfd) { + // send(i, buf, n, 0); + // } + // } + // }; + // exit(0); + // } + + pthread_t client_thread; + pthread_create(&client_thread, NULL, &handle_client, &connfd); + } + return 0; +} + +void* handle_client(void* param) { + int *connfd = (int *) param; + int n; + char buf[100]; + while ((n = recv(*connfd, buf, 100, 0)) > 0) { + printf("Client message (%d): %s", *connfd, buf); + fflush(stdout); + for (int i = 0; i < client_list_len; i++) { + int curr_conn = *(client_list + i); + if (curr_conn != *connfd) { + send(curr_conn, buf, n, 0); + } + } + } + return 0; +} 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 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]) { + const char *server_name = "127.0.0.1"; + int sockfd, connfd; + struct sockaddr_in servaddr, cli; + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + servaddr.sin_family = AF_INET; + inet_pton(AF_INET, server_name, &servaddr.sin_addr); + + int port = atoi(argv[1]); + servaddr.sin_port = htons(port); + + if((connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) == 0) { + printf("Connected to server!\n"); + fflush(stdout); + } else { + printf("Invalid or busy server address\n"); + fflush(stdout); + exit(0); + }; + + int cont = 0; + char *buf = malloc(1024); + printf("Client input: "); + fgets(buf, 100, stdin); + while(strcmp(buf, "quit\n") != 0) { + send(sockfd, buf, 100, 0); + if ((cont = recv(sockfd, buf, 100, 0)) > 0) { + printf("Server response: %s\n", buf);; + fflush(stdout); + } + printf("Client input: "); + fgets(buf, 100, stdin); + }; + + close(sockfd); +} 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 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void* handle_client(void *); + +int client_list[16]; +int client_list_len; + +int main(int argc, char *argv[]) { + int sockfd; + + pthread_mutex_t list_lock; + pthread_mutex_init(&list_lock, NULL); + + struct sockaddr_in servaddr, cli; + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + + int port = atoi(argv[1]); + servaddr.sin_port = htons(port); + + bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); + + listen(sockfd, 128); + printf("Server started at %s...\n", inet_ntoa(servaddr.sin_addr)); + fflush(stdout); + + while(1) { + socklen_t len = sizeof(cli); + int connfd = accept(sockfd, (struct sockaddr*)&cli, &len); + int i = getpeername(connfd, (struct sockaddr*)&cli, &len); + + client_list[client_list_len] = connfd; + client_list_len += 1; + + printf("Clients connected: %d \nDescriptors: ", client_list_len); + for(int i = 0; i < client_list_len; i++) { + printf("%d ", client_list[i]); + } + printf("\n"); + fflush(stdout); + + pthread_t client_thread; + pthread_create(&client_thread, NULL, &handle_client, &connfd); + } + close(sockfd); + return 0; +} + +void* handle_client(void* param) { + int *connfd = (int *) param; + int n; + char buf[100]; + bzero(&buf, 100); + while ((n = recv(*connfd, buf, 100, 0)) > 0) { + printf("Client message (%d): %s", *connfd, buf); + fflush(stdout); + send(*connfd, buf, n, 0); + bzero(&buf, 100); + } + close(*connfd); + return 0; +} -- cgit v1.2.3