-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
48 lines (41 loc) · 1 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
#include <stdio.h>
#include <stdlib.h>
#include <wait.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
int main() {
int fd = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
addr.sin_port = htons(9999);
addr.sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &(addr.sin_addr.s_addr));
int ret = connect(fd, (struct sockaddr*)&addr, sizeof addr);
if (ret == -1) {
perror("connect");
exit(-1);
}
char buf[5];
while (1) {
bzero(buf, sizeof buf);
fgets(buf, sizeof buf, stdin);
ret = write(fd, buf, strlen(buf) + 1);
if (ret == -1) {
perror("write");
exit(-1);
}
int len = read(fd, buf, sizeof buf);
if (len == -1) {
perror("read");
exit(-1);
}
else if (len == 0) {
printf("server lose ...\n");
}
else {
printf("Data : %s", buf);
}
}
close(fd);
return 0;
}