Rev 9558 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
8687 | turbocat | 1 | #include |
2 | #include |
||
3 | #include |
||
4 | #include |
||
5 | |||
6 | int main() { |
||
7 | char *host = "kolibrios.org"; |
||
8 | int port = 80; |
||
9 | printf("Connecting to %s on port %d\n", host, port); |
||
10 | struct addrinfo *addr_info; |
||
11 | char port_str[16]; sprintf(port_str, "%d", port); |
||
12 | struct addrinfo hints; |
||
13 | memset(&hints, 0, sizeof(hints)); |
||
14 | hints.ai_family = AF_UNSPEC; // IPv4 or IPv6 doesnt matter |
||
15 | hints.ai_socktype = SOCK_STREAM; // TCP stream sockets |
||
16 | if (getaddrinfo(host, port_str, 0, &addr_info) != 0) { |
||
17 | printf("Host %s not found!\n", host); |
||
18 | freeaddrinfo(addr_info); |
||
19 | exit(-1); |
||
20 | } |
||
21 | printf("IP address of %s is %s\n", host, inet_ntoa(addr_info->ai_addr->sin_addr)); |
||
22 | //printf("Host port = %d\n", addr_info->ai_addr->sin_port >> 8); |
||
23 | |||
24 | char request[256]; |
||
25 | sprintf(request, "GET /en/ HTTP/1.1\r\nHost: %s\r\n\r\n", host); |
||
26 | printf("request = %s\n", request); |
||
27 | |||
28 | int sock = socket(AF_INET4, SOCK_STREAM, IPPROTO_TCP); |
||
29 | |||
30 | puts("Connecting...\n"); |
||
31 | if (connect(sock, addr_info->ai_addr, addr_info->ai_addrlen) != 0) { |
||
32 | printf("Connection failed, errno = %d\n", errno); |
||
33 | exit(errno); |
||
34 | } |
||
35 | puts("Connected successfully\n"); |
||
36 | |||
37 | puts("Sending request...\n"); |
||
38 | if (send(sock, request, strlen(request), MSG_NOFLAG) == -1) { |
||
39 | printf("Sending failed, errno = %d\n", errno); |
||
40 | exit(errno); |
||
41 | } |
||
42 | puts("Request sended successfully, waiting for response...\n"); |
||
43 | |||
44 | char buf[512 + 1]; |
||
45 | if (recv(sock, buf, 512, MSG_NOFLAG) == -1) { |
||
46 | printf("Receive failed, errno = %d\n", errno); |
||
47 | exit(errno); |
||
48 | } |
||
49 | |||
50 | printf("Response = %s\n", buf); |
||
51 | |||
52 | freeaddrinfo(addr_info); |
||
53 | |||
54 | close(sock); |
||
55 | puts("\n goodbye)\n"); |
||
56 | exit(0); |
||
57 | } |