Subversion Repositories Kolibri OS

Rev

Rev 9620 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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