diff options
Diffstat (limited to 'q1/client.c')
-rw-r--r-- | q1/client.c | 42 |
1 files changed, 42 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 | |||
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 | } | ||