aboutsummaryrefslogtreecommitdiff
path: root/q1
diff options
context:
space:
mode:
Diffstat (limited to 'q1')
-rw-r--r--q1/client.c42
-rw-r--r--q1/makefile12
-rw-r--r--q1/server.c64
3 files changed, 118 insertions, 0 deletions
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
10int 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 @@
1all: server.c client.c
2 gcc server.c -o server
3 gcc client.c -o client
4
5server: server.c
6 gcc server.c -o server
7
8client: client.c
9 gcc client.c -o client
10
11clean:
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
10int 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}