-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
97 lines (63 loc) · 1.59 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "socket.h"
#define BUFFER_LEN 256
int main(int argc, char** argv) {
if(argc != 3) {
fprintf(stderr, "Usage: %s <server name> <port>\n", argv[0]);
exit(1);
}
// Read command line arguments
char* server_name = argv[1];
unsigned short port = atoi(argv[2]);
// Connect to the server
int socket_fd = socket_connect(server_name, port);
if(socket_fd == -1) {
perror("Failed to connect");
exit(2);
}
// Set up file streams
FILE* to_server = fdopen(dup(socket_fd), "wb");
if(to_server == NULL) {
perror("Failed to open stream to server");
exit(2);
}
FILE* from_server = fdopen(dup(socket_fd), "rb");
if(from_server == NULL) {
perror("Failed to open stream from server");
exit(2);
}
int going = 1;
while(going){
size_t buf_size = BUFFER_LEN;
char* msg = (char*) malloc(buf_size * sizeof(char));
getline(&msg, &buf_size, stdin);
fprintf(to_server, "%s", msg);
fflush(to_server);
// Read a message from the server
char buffer[BUFFER_LEN];
if(fgets(buffer, BUFFER_LEN, from_server) == NULL) {
perror("Reading from server failed");
exit(2);
}
if(strcmp(buffer, "quit\n") == 0){
going = 0;
break;
}
printf("Server sent: %s\n", buffer);
/**
// Send a message to the server
fprintf(to_server, "Hello server!\n");
// Flush the output buffer
fflush(to_server);
**/
}
// Close file streams
fclose(to_server);
fclose(from_server);
// Close socket
close(socket_fd);
return 0;
}