From 372ffa5ec3ce365b0a243ac2b0128496542cae6e Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 21 Oct 2020 15:30:03 +0530 Subject: init --- q1/client.c | 42 ++++++++++++++++++++++++++++++++++++++++ q1/makefile | 12 ++++++++++++ q1/server.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 q1/client.c create mode 100644 q1/makefile create mode 100644 q1/server.c (limited to 'q1') diff --git a/q1/client.c b/q1/client.c new file mode 100644 index 0000000..a242e42 --- /dev/null +++ b/q1/client.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + if (argc < 2) { + printf("Requires atleast 1 argument!\n"); + return -1; + } + 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 = 0; + printf("Enter port to connect to: "); + scanf("%d", &port); + fflush(stdout); + fflush(stdin); + servaddr.sin_port = htons(port); + + connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)); + printf("Connected to server!\n"); + fflush(stdout); + + char file_contents[100]; + + send(sockfd, argv[1], strlen(argv[1]), 0); + int cont; + while((cont=(recv(sockfd, file_contents, 100, 0))) > 0) { + printf("File contents: %s", file_contents); + } + close(sockfd); +} diff --git a/q1/makefile b/q1/makefile new file mode 100644 index 0000000..e890624 --- /dev/null +++ b/q1/makefile @@ -0,0 +1,12 @@ +all: server.c client.c + gcc server.c -o server + gcc client.c -o client + +server: server.c + gcc server.c -o server + +client: client.c + gcc client.c -o client + +clean: + rm server client *.out diff --git a/q1/server.c b/q1/server.c new file mode 100644 index 0000000..92dfad6 --- /dev/null +++ b/q1/server.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main() { + 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 = 0; + printf("Enter port to bind to: "); + scanf("%d", &port); + fflush(stdout); + fflush(stdin); + + servaddr.sin_port = htons(port); + + bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); + + listen(sockfd, 128); + socklen_t len = sizeof(cli); + printf("Server started at %s...\n", inet_ntoa(servaddr.sin_addr)); + fflush(stdout); + + connfd = accept(sockfd, (struct sockaddr *)&cli, &len); + if (connfd < 0) { + printf("Couldn't open socket!\n"); + close(sockfd); + return -1; + } + + printf("Client connected with IP address: %s\n", inet_ntoa(cli.sin_addr)); + fflush(stdout); + + char filename[100]; + bzero(filename, 100); + int n; + n = recv(connfd, filename, 100, 0); + printf("received: '%s'", filename); + fflush(stdout); + + FILE *request_file = fopen(filename, "r"); + if (request_file == NULL) { + printf("File not found: %s", filename); + send(connfd, "file not found", 14, 0); + } else { + char file_contents[500]; + while(fgets(file_contents, 100, request_file)) { + fflush(stdout); + send(connfd, file_contents, 100, 0); + }; + fclose(request_file); + } + fflush(stdout); + close(sockfd); +} -- cgit v1.2.3