diff options
-rw-r--r-- | .gitignore | 53 | ||||
-rw-r--r-- | q1/client.c | 42 | ||||
-rw-r--r-- | q1/makefile | 12 | ||||
-rw-r--r-- | q1/server.c | 64 | ||||
-rw-r--r-- | readme.txt | 9 | ||||
-rw-r--r-- | shell.nix | 8 |
6 files changed, 188 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dab799c --- /dev/null +++ b/.gitignore | |||
@@ -0,0 +1,53 @@ | |||
1 | # Prerequisites | ||
2 | *.d | ||
3 | |||
4 | # Object files | ||
5 | *.o | ||
6 | *.ko | ||
7 | *.obj | ||
8 | *.elf | ||
9 | |||
10 | # Linker output | ||
11 | *.ilk | ||
12 | *.map | ||
13 | *.exp | ||
14 | |||
15 | # Precompiled Headers | ||
16 | *.gch | ||
17 | *.pch | ||
18 | |||
19 | # Libraries | ||
20 | *.lib | ||
21 | *.a | ||
22 | *.la | ||
23 | *.lo | ||
24 | |||
25 | # Shared objects (inc. Windows DLLs) | ||
26 | *.dll | ||
27 | *.so | ||
28 | *.so.* | ||
29 | *.dylib | ||
30 | |||
31 | # Executables | ||
32 | *.exe | ||
33 | *.out | ||
34 | *.app | ||
35 | *.i*86 | ||
36 | *.x86_64 | ||
37 | *.hex | ||
38 | |||
39 | # Debug files | ||
40 | *.dSYM/ | ||
41 | *.su | ||
42 | *.idb | ||
43 | *.pdb | ||
44 | |||
45 | # Kernel Module Compile Results | ||
46 | *.mod* | ||
47 | *.cmd | ||
48 | .tmp_versions/ | ||
49 | modules.order | ||
50 | Module.symvers | ||
51 | Mkfile.old | ||
52 | dkms.conf | ||
53 | |||
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 @@ | |||
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 | |||
10 | int main(int argc, char *argv[]) { | ||
11 | if (argc < 2) { | ||
12 | printf("Requires atleast 1 argument!\n"); | ||
13 | return -1; | ||
14 | } | ||
15 | const char *server_name = "127.0.0.1"; | ||
16 | int sockfd, connfd; | ||
17 | struct sockaddr_in servaddr, cli; | ||
18 | |||
19 | sockfd = socket(AF_INET, SOCK_STREAM, 0); | ||
20 | servaddr.sin_family = AF_INET; | ||
21 | inet_pton(AF_INET, server_name, &servaddr.sin_addr); | ||
22 | |||
23 | int port = 0; | ||
24 | printf("Enter port to connect to: "); | ||
25 | scanf("%d", &port); | ||
26 | fflush(stdout); | ||
27 | fflush(stdin); | ||
28 | servaddr.sin_port = htons(port); | ||
29 | |||
30 | connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)); | ||
31 | printf("Connected to server!\n"); | ||
32 | fflush(stdout); | ||
33 | |||
34 | char file_contents[100]; | ||
35 | |||
36 | send(sockfd, argv[1], strlen(argv[1]), 0); | ||
37 | int cont; | ||
38 | while((cont=(recv(sockfd, file_contents, 100, 0))) > 0) { | ||
39 | printf("File contents: %s", file_contents); | ||
40 | } | ||
41 | close(sockfd); | ||
42 | } | ||
diff --git a/q1/makefile b/q1/makefile new file mode 100644 index 0000000..e890624 --- /dev/null +++ b/q1/makefile | |||
@@ -0,0 +1,12 @@ | |||
1 | all: server.c client.c | ||
2 | gcc server.c -o server | ||
3 | gcc client.c -o client | ||
4 | |||
5 | server: server.c | ||
6 | gcc server.c -o server | ||
7 | |||
8 | client: client.c | ||
9 | gcc client.c -o client | ||
10 | |||
11 | clean: | ||
12 | 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 @@ | |||
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 | |||
10 | int main() { | ||
11 | int sockfd, connfd; | ||
12 | struct sockaddr_in servaddr, cli; | ||
13 | |||
14 | sockfd = socket(AF_INET, SOCK_STREAM, 0); | ||
15 | servaddr.sin_family = AF_INET; | ||
16 | servaddr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
17 | |||
18 | int port = 0; | ||
19 | printf("Enter port to bind to: "); | ||
20 | scanf("%d", &port); | ||
21 | fflush(stdout); | ||
22 | fflush(stdin); | ||
23 | |||
24 | servaddr.sin_port = htons(port); | ||
25 | |||
26 | bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); | ||
27 | |||
28 | listen(sockfd, 128); | ||
29 | socklen_t len = sizeof(cli); | ||
30 | printf("Server started at %s...\n", inet_ntoa(servaddr.sin_addr)); | ||
31 | fflush(stdout); | ||
32 | |||
33 | connfd = accept(sockfd, (struct sockaddr *)&cli, &len); | ||
34 | if (connfd < 0) { | ||
35 | printf("Couldn't open socket!\n"); | ||
36 | close(sockfd); | ||
37 | return -1; | ||
38 | } | ||
39 | |||
40 | printf("Client connected with IP address: %s\n", inet_ntoa(cli.sin_addr)); | ||
41 | fflush(stdout); | ||
42 | |||
43 | char filename[100]; | ||
44 | bzero(filename, 100); | ||
45 | int n; | ||
46 | n = recv(connfd, filename, 100, 0); | ||
47 | printf("received: '%s'", filename); | ||
48 | fflush(stdout); | ||
49 | |||
50 | FILE *request_file = fopen(filename, "r"); | ||
51 | if (request_file == NULL) { | ||
52 | printf("File not found: %s", filename); | ||
53 | send(connfd, "file not found", 14, 0); | ||
54 | } else { | ||
55 | char file_contents[500]; | ||
56 | while(fgets(file_contents, 100, request_file)) { | ||
57 | fflush(stdout); | ||
58 | send(connfd, file_contents, 100, 0); | ||
59 | }; | ||
60 | fclose(request_file); | ||
61 | } | ||
62 | fflush(stdout); | ||
63 | close(sockfd); | ||
64 | } | ||
diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..62fd58d --- /dev/null +++ b/readme.txt | |||
@@ -0,0 +1,9 @@ | |||
1 | Akshay's NPS lab programs | ||
2 | ------------------------- | ||
3 | |||
4 | Q1: Write a networking program consisting of a client and | ||
5 | server components. The client must request for a file by | ||
6 | providing a file name, and the server must respond with the | ||
7 | contents of the file or an appropriate error message. Bonus: | ||
8 | Modify the client to accept the filname via a command line | ||
9 | 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 @@ | |||
1 | { pkgs ? import <nixpkgs> {} }: | ||
2 | |||
3 | pkgs.mkShell { | ||
4 | buildInputs = with pkgs; [ | ||
5 | gcc | ||
6 | clang-tools | ||
7 | ]; | ||
8 | } | ||