-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
54 lines (40 loc) · 933 Bytes
/
client.c
File metadata and controls
54 lines (40 loc) · 933 Bytes
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
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include "handler/error.h"
#include "handler/socket.h"
#define BUF_SIZE (4096)
int main(int argc, char *argv[])
{
int sock;
char buf[BUF_SIZE];
int wr_len, rd_len, count;
if (argc != 3)
err_msg("usage: %s <ip address> <port>", ERR_DNG, argv[0]);
sock = connect_socket(argv[1], atoi(argv[2]));
if (sock < 0)
err_msg("connect_socket() error: %d", ERR_CTC, sock);
while (true)
{
fputs("Input: ", stdout);
fgets(buf, BUF_SIZE, stdin);
if (strlen(buf) == 1)
break;
wr_len = write(sock, buf, strlen(buf));
rd_len = 0;
while (rd_len < wr_len) {
count = read(sock, &buf[rd_len], BUF_SIZE - 1);
if (count == -1) {
err_msg("read() error", ERR_CTC);
goto BREAK;
}
rd_len += count;
}
buf[rd_len] = '\0';
printf("Message: %s", buf);
} BREAK:
close(sock);
return 0;
}