|
6 | 6 | #include <netinet/in.h> //sockaddr_in |
7 | 7 | #include <sys/socket.h> |
8 | 8 | #include <unistd.h> |
| 9 | +#include <netdb.h> |
9 | 10 |
|
10 | | -#define RECV_BUFF_SIZE 128 |
| 11 | +#define RECV_BUFF_SIZE 128 |
| 12 | +#define SEND_BUF_SIZE 512 |
11 | 13 |
|
12 | | -int main() |
| 14 | +int main(int argc, const char *argv[]) |
13 | 15 | { |
14 | | - int client_fd; // 客户端socket |
15 | | - struct sockaddr_in s_addr; // 目标主机地址 |
16 | | - char * data_to_send = "GET / HTTP/1.1\r\n" // 要发送到数据 |
17 | | - "Host: www.baidu.com\r\n" |
18 | | - "Accept: */*\r\n" |
19 | | - "\r\n\r\n"; |
20 | | - size_t to_send_size = strlen(data_to_send); // 要发送数据的大小 |
21 | | - ssize_t sent_size = 0; // 实际发送的大小 |
| 16 | + const char* host = argv[1]; // 目标主机 |
| 17 | + struct addrinfo hints; // 填充getaddrinfo参数 |
| 18 | + struct addrinfo *result; // 存放getaddrinfo返回数据 |
| 19 | + int r = 0; // 临时存放函数返回值 |
| 20 | + ssize_t sent_size = 0; // 实际发送的大小 |
| 21 | + char send_buff[SEND_BUF_SIZE]; // 发送缓冲区 |
| 22 | + const char *send_tpl; // 数据模板,%s是host占位符 |
| 23 | + size_t to_send_size = 0; // 要发送到数据大小 |
| 24 | + int client_fd; // 客户端socket |
| 25 | + char data_to_recv[RECV_BUFF_SIZE]; // 数据接收缓冲区 |
| 26 | + ssize_t recv_size; // 已接受到的数据大小 |
| 27 | + int i; // 循环变量 |
| 28 | + |
| 29 | + if (argc != 2) { |
| 30 | + printf("Usage:%s [host]\n", argv[0]); |
| 31 | + return 1; |
| 32 | + } |
| 33 | + |
| 34 | + send_tpl = "GET / HTTP/1.1\r\n" |
| 35 | + "Host: %s\r\n" |
| 36 | + "Accept: */*\r\n" |
| 37 | + "\r\n\r\n"; |
| 38 | + |
| 39 | + |
| 40 | + memset(&hints, 0, sizeof(struct addrinfo)); |
| 41 | + hints.ai_family = AF_UNSPEC; |
| 42 | + hints.ai_socktype = SOCK_STREAM; |
| 43 | + hints.ai_flags = 0; |
| 44 | + hints.ai_protocol = 0; |
| 45 | + |
| 46 | + r = getaddrinfo(host, "80", &hints, &result); |
| 47 | + if (r != 0) { |
| 48 | + printf("getaddrinfo error"); |
| 49 | + return 3; |
| 50 | + } |
22 | 51 |
|
23 | | - char data_to_recv[RECV_BUFF_SIZE]; // 数据接收缓冲区 |
24 | | - ssize_t recv_size; // 已接受到的数据大小 |
25 | 52 |
|
26 | | - int i; // 循环变量 |
| 53 | + |
| 54 | + // 构建发送缓冲区 |
| 55 | + if (strlen(host) + strlen(send_tpl) > SEND_BUF_SIZE - 2) { // 2 = strlen("%s") |
| 56 | + printf("host too long.\n"); |
| 57 | + return 2; |
| 58 | + } |
| 59 | + |
| 60 | + to_send_size = snprintf(send_buff, SEND_BUF_SIZE, send_tpl, host); |
| 61 | + if (to_send_size < 0) { |
| 62 | + printf("snprintf error:.\n"); |
| 63 | + return 3; |
| 64 | + } |
| 65 | + |
| 66 | + |
27 | 67 |
|
28 | 68 | // 创建socket |
29 | | - if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
| 69 | + if ((client_fd = socket(result->ai_family, result->ai_socktype, |
| 70 | + result->ai_protocol)) == -1) { |
30 | 71 | printf("create socket error:%d\n", client_fd); |
31 | 72 | return -1; |
32 | 73 | } |
33 | 74 |
|
34 | 75 | printf("cerate socket ok: %d\n", client_fd); |
35 | 76 |
|
36 | | - // 构建目标主机地址 |
37 | | - memset(&s_addr, 0, sizeof(struct sockaddr_in)); |
38 | | - s_addr.sin_family = AF_INET; |
39 | | - s_addr.sin_addr.s_addr = inet_addr("180.97.33.71"); |
40 | | - s_addr.sin_port = htons(80); |
41 | | - printf("will collect s_addr=%#x, port=%#x\n", (unsigned int)s_addr.sin_addr.s_addr, |
42 | | - (unsigned int)s_addr.sin_port); |
43 | | - |
44 | 77 | // 连接目标主机 |
45 | | - if (connect(client_fd, (struct sockaddr *)(&s_addr), (socklen_t)sizeof(struct sockaddr)) == -1) { |
| 78 | + r = connect(client_fd, result->ai_addr, result->ai_addrlen); |
| 79 | + freeaddrinfo(result); |
| 80 | + if (r == -1) { |
46 | 81 | printf("connect error.\n"); |
47 | 82 | return -2; |
48 | 83 | } |
49 | 84 | printf("collect ok\n"); |
50 | 85 |
|
51 | 86 | // 发送数据 |
52 | | - printf("will send:\n%s", data_to_send); |
53 | | - sent_size = write(client_fd, data_to_send, (size_t)strlen(data_to_send)); |
| 87 | + printf("will send:\n%s", send_buff); |
| 88 | + sent_size = write(client_fd, send_buff, to_send_size); |
54 | 89 | if (sent_size < 0) { |
55 | 90 | printf("send data error.\n"); |
56 | 91 | return -3; |
|
0 commit comments