aboutsummaryrefslogtreecommitdiff
path: root/q6/a/client.c
diff options
context:
space:
mode:
Diffstat (limited to 'q6/a/client.c')
-rw-r--r--q6/a/client.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/q6/a/client.c b/q6/a/client.c
new file mode 100644
index 0000000..7219476
--- /dev/null
+++ b/q6/a/client.c
@@ -0,0 +1,47 @@
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#include <string.h>
10
11int main(int argc, char* argv[]) {
12 const char *server_name = "127.0.0.1";
13 int sockfd, connfd;
14 struct sockaddr_in servaddr, cli;
15
16 sockfd = socket(AF_INET, SOCK_STREAM, 0);
17 servaddr.sin_family = AF_INET;
18 inet_pton(AF_INET, server_name, &servaddr.sin_addr);
19
20 int port = atoi(argv[1]);
21 servaddr.sin_port = htons(port);
22
23 if((connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) == 0) {
24 printf("Connected to server!\n");
25 fflush(stdout);
26 } else {
27 printf("Invalid or busy server address\n");
28 fflush(stdout);
29 exit(0);
30 };
31
32 int cont = 0;
33 char *buf = malloc(1024);
34 printf("Client input: ");
35 fgets(buf, 100, stdin);
36 while(strcmp(buf, "quit\n") != 0) {
37 send(sockfd, buf, 100, 0);
38 if ((cont = recv(sockfd, buf, 100, 0)) > 0) {
39 printf("Server response: %s\n", buf);;
40 fflush(stdout);
41 }
42 printf("Client input: ");
43 fgets(buf, 100, stdin);
44 };
45
46 close(sockfd);
47}