From a903588ed52a877db413f72a018570fb6810ad2c Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 4 Nov 2020 19:21:35 +0530 Subject: add concurrent, iterative and UDP servers --- q6/a/client.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ q6/a/server.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ q6/b/client.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ q6/b/server.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ q6/c/client.c | 41 +++++++++++++++++++++++++++++++++++++++++ q6/c/server.c | 38 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 266 insertions(+) create mode 100644 q6/a/client.c create mode 100644 q6/a/server.c create mode 100644 q6/b/client.c create mode 100644 q6/b/server.c create mode 100644 q6/c/client.c create mode 100644 q6/c/server.c (limited to 'q6') 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 @@ +#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/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 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + int sockfd, connfd; + 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); + + printf("Client connected at port %d\n", htons(cli.sin_port)); + int n; + char buf[100]; + + int pid; + if((pid=fork())==0){ + close(sockfd); + while((n = recv(connfd, buf, 100, 0)) > 0) { + printf("Client message: %s\n", buf); + fflush(stdout); + send(connfd, buf, n, 0); + }; + exit(0); + } + close(connfd); + } + return 0; +} 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 @@ +#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/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 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + int sockfd, connfd; + 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); + + printf("Client connected at port %d\n", htons(cli.sin_port)); + int n; + char buf[100]; + + while((n = recv(connfd, buf, 100, 0)) > 0) { + printf("Client message: %s\n", buf); + fflush(stdout); + send(connfd, buf, n, 0); + }; + close(connfd); + } + return 0; +} 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 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + int sockfd; + struct sockaddr_in servaddr; + sockfd = socket(AF_INET, SOCK_DGRAM, 0); + + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + + int port = atoi(argv[1]); + servaddr.sin_port = htons(port); + + int n; + + socklen_t len = sizeof(servaddr); + char *buf = malloc(100); + fgets(buf, 100, stdin); + while(strcmp(buf, "quit\n") != 0) { + sendto(sockfd, buf, strlen(buf), + MSG_CONFIRM, (const struct sockaddr *) &servaddr, + sizeof(servaddr)); + + n = recvfrom(sockfd, buf, 100, + MSG_WAITALL, (struct sockaddr *) &servaddr, + &len); + printf("Recieved: %s", buf); + fgets(buf, 100, stdin); + } + + close(sockfd); + return 0; +} 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 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + int sockfd; + struct sockaddr_in servaddr, cli; + sockfd = socket(AF_INET, SOCK_DGRAM, 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)); + + socklen_t len = sizeof(cli); + while(1) { + int n; + char *buf = malloc(100); + while( + (n = recvfrom(sockfd, (char *)buf, 100, MSG_WAITALL, (struct sockaddr*) &cli, &len)) + > 0 + ) { + printf("Client message: %s\n", buf); + fflush(stdout); + sendto(sockfd, buf, n, MSG_CONFIRM, (const struct sockaddr *) &cli, len); + } + } + close(sockfd); + return 0; +} -- cgit v1.2.3