Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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