-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.c
More file actions
65 lines (51 loc) · 1.15 KB
/
cli.c
File metadata and controls
65 lines (51 loc) · 1.15 KB
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
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#define err_exit() \
do { \
printf("%s:%d:%s\n", __func__, __LINE__, strerror(errno)); \
exit(-1); \
} while(0)
int main(int argc, char **argv)
{
int n, sockfd;
char buf[128];
struct sockaddr_in srv_addr;
socklen_t addr_len;
addr_len = sizeof(srv_addr);
if (argc < 3) {
printf("Usage:%s serverip port\n", argv[0]);
exit(0);
}
bzero(&srv_addr, addr_len);
srv_addr.sin_family = AF_INET;
inet_pton(AF_INET, argv[1], &srv_addr.sin_addr);
srv_addr.sin_port = htons(atoi(argv[2]));
while (1) {
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
err_exit();
}
if (connect(sockfd, (const struct sockaddr *)&srv_addr, addr_len)) {
err_exit();
}
if (-1 == (n = read(fileno(stdin), buf, 128)))
err_exit();
buf[n] = '\0';
if (-1 == write(sockfd, buf, n))
err_exit();
if ((n = read(sockfd, buf, 128)) < 0) {
err_exit();
}
buf[n] = '\0';
printf("recv:%s\n", buf);
close(sockfd);
}
return 0;
}