2021-04-27 18:33:31 +02:00
|
|
|
#include <clayer/network.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2022-04-15 11:11:49 +02:00
|
|
|
#include <string.h>
|
2021-04-27 18:33:31 +02:00
|
|
|
|
2022-04-15 11:11:49 +02:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
char* host = "kolibrios.org";
|
2021-04-27 18:33:31 +02:00
|
|
|
int port = 80;
|
|
|
|
printf("Connecting to %s on port %d\n", host, port);
|
2022-04-15 11:11:49 +02:00
|
|
|
struct addrinfo* addr_info;
|
|
|
|
char port_str[16];
|
|
|
|
sprintf(port_str, "%d", port);
|
2021-04-27 18:33:31 +02:00
|
|
|
struct addrinfo hints;
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
2022-04-15 11:11:49 +02:00
|
|
|
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6 doesnt matter
|
2021-04-27 18:33:31 +02:00
|
|
|
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
|
|
|
|
if (getaddrinfo(host, port_str, 0, &addr_info) != 0) {
|
|
|
|
printf("Host %s not found!\n", host);
|
|
|
|
freeaddrinfo(addr_info);
|
|
|
|
exit(-1);
|
|
|
|
}
|
2022-04-15 11:11:49 +02:00
|
|
|
printf("IP address of %s is %s\n", host, inet_ntoa(addr_info->ai_addr->sin_addr));
|
|
|
|
// printf("Host port = %d\n", addr_info->ai_addr->sin_port >> 8);
|
2021-04-27 18:33:31 +02:00
|
|
|
|
|
|
|
char request[256];
|
|
|
|
sprintf(request, "GET /en/ HTTP/1.1\r\nHost: %s\r\n\r\n", host);
|
|
|
|
printf("request = %s\n", request);
|
|
|
|
|
|
|
|
int sock = socket(AF_INET4, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
|
|
|
|
puts("Connecting...\n");
|
|
|
|
if (connect(sock, addr_info->ai_addr, addr_info->ai_addrlen) != 0) {
|
2022-04-15 11:11:49 +02:00
|
|
|
printf("Connection failed, errno = %d\n", errno);
|
|
|
|
exit(errno);
|
2021-04-27 18:33:31 +02:00
|
|
|
}
|
|
|
|
puts("Connected successfully\n");
|
|
|
|
|
|
|
|
puts("Sending request...\n");
|
|
|
|
if (send(sock, request, strlen(request), MSG_NOFLAG) == -1) {
|
2022-04-15 11:11:49 +02:00
|
|
|
printf("Sending failed, errno = %d\n", errno);
|
|
|
|
exit(errno);
|
2021-04-27 18:33:31 +02:00
|
|
|
}
|
|
|
|
puts("Request sended successfully, waiting for response...\n");
|
|
|
|
|
|
|
|
char buf[512 + 1];
|
|
|
|
if (recv(sock, buf, 512, MSG_NOFLAG) == -1) {
|
2022-04-15 11:11:49 +02:00
|
|
|
printf("Receive failed, errno = %d\n", errno);
|
|
|
|
exit(errno);
|
2021-04-27 18:33:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Response = %s\n", buf);
|
|
|
|
|
|
|
|
freeaddrinfo(addr_info);
|
|
|
|
|
|
|
|
close(sock);
|
|
|
|
puts("\n goodbye)\n");
|
|
|
|
exit(0);
|
|
|
|
}
|