-
Notifications
You must be signed in to change notification settings - Fork 1
/
t_back_client_0.cpp
45 lines (37 loc) · 1020 Bytes
/
t_back_client_0.cpp
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
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <cassert>
int main(int argc, char *argv[]) {
assert(argc == 2);
std::cout << argv[1] << std::endl;
int client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == client_fd) {
perror("创建socket失败");
exit(1);
}
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(65530);
inet_pton(AF_INET, argv[1], &serv_addr.sin_addr);
if (connect(client_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("连接失败");
exit(1);
}
std::string douniwan;
char buf[1024];
while (1) {
int nread = read(client_fd, buf, 1024);
if (nread <= 0)
break;
buf[nread] = '\0';
std::cout << buf << std::endl;
}
close(client_fd);
return 0;
}