From 372ffa5ec3ce365b0a243ac2b0128496542cae6e Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 21 Oct 2020 15:30:03 +0530 Subject: init --- .gitignore | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ q1/client.c | 42 ++++++++++++++++++++++++++++++++++++++++ q1/makefile | 12 ++++++++++++ q1/server.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ readme.txt | 9 +++++++++ shell.nix | 8 ++++++++ 6 files changed, 188 insertions(+) create mode 100644 .gitignore create mode 100644 q1/client.c create mode 100644 q1/makefile create mode 100644 q1/server.c create mode 100644 readme.txt create mode 100644 shell.nix diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dab799c --- /dev/null +++ b/.gitignore @@ -0,0 +1,53 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + 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); +} diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..62fd58d --- /dev/null +++ b/readme.txt @@ -0,0 +1,9 @@ +Akshay's NPS lab programs +------------------------- + +Q1: Write a networking program consisting of a client and +server components. The client must request for a file by +providing a file name, and the server must respond with the +contents of the file or an appropriate error message. Bonus: +Modify the client to accept the filname via a command line +argument. diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..67483ce --- /dev/null +++ b/shell.nix @@ -0,0 +1,8 @@ +{ pkgs ? import {} }: + +pkgs.mkShell { + buildInputs = with pkgs; [ + gcc + clang-tools + ]; +} -- cgit v1.2.3