-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathhttpclient.c
86 lines (79 loc) · 2.34 KB
/
httpclient.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* Copyright (c) [2023] [Syswonder Community]
* [Ruxos] is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/uio.h>
const char request[] = "\
GET / HTTP/1.1\r\n\
Host: ident.me\r\n\
Accept: */*\r\n\
\r\n";
char request1[] = "\
GET / HTTP/1.1\r\n";
char request2[] = "Host: ident.me\r\n\
Accept: */*\r\n\
\r\n";
int main()
{
puts("Hello, Ruxos C HTTP client!");
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == -1) {
perror("socket() error");
return -1;
}
struct addrinfo *res;
if (getaddrinfo("ident.me", NULL, NULL, &res) != 0) {
perror("getaddrinfo() error");
return -1;
}
char str[INET_ADDRSTRLEN];
if (inet_ntop(AF_INET, &(((struct sockaddr_in *)(res->ai_addr))->sin_addr), str,
INET_ADDRSTRLEN) == NULL) {
perror("inet_ntop() error");
return -1;
}
printf("IP: %s\n", str);
((struct sockaddr_in *)(res->ai_addr))->sin_port = htons(80);
if (connect(sock, res->ai_addr, sizeof(*(res->ai_addr))) != 0) {
perror("connect() error");
return -1;
}
char rebuf[2000] = {};
if (send(sock, request, strlen(request), 0) == -1) {
perror("send() error");
return -1;
}
ssize_t l = recv(sock, rebuf, 2000, 0);
if (l == -1) {
perror("recv() error");
return -1;
}
rebuf[l] = '\0';
printf("%s\n", rebuf);
// test sendmsg
struct iovec iovs[2] = {
{ .iov_base = request1, .iov_len = strlen(request1)},
{ .iov_base = request2, .iov_len = strlen(request2)}
};
struct msghdr mg = {
.msg_iov = iovs,
.msg_iovlen = 2
};
int num = sendmsg(sock, &mg, 0);
if (num == -1) {
perror("sendmsg() error");
return -1;
}
freeaddrinfo(res);
return 0;
}