Thread: http requests

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    http requests

    Hello. I am trying to write a program that will send an http request to a web server and then get the requested resource. I know a little about socket programming, but not much. I got stuck when i started trying to connect to a server:

    Code:
    SOCKET call_socket (const char *hostname, unsigned short portnum)
    {
     struct sockaddr_in sa;
     struct hostent     *hp;
     SOCKET s;
     
     hp = gethostbyname (hostname);
     if (hp == NULL)
      return INVALID_SOCKET;
     
     memset (&sa, 0, sizeof (sa));
     memcpy ((char *) &sa.sin_addr, hp->h_addr, hp->h_length);
    
     sa.sin_family = hp->h_addrtype;
     sa.sin_port = htons ((u_short) portnum);
     
     s = socket (hp->h_addrtype, SOCK_STREAM, 0);
     if (s == INVALID_SOCKET)
     {
      closesocket(s);
      return INVALID_SOCKET;
     }
     
     if (connect (s, (struct sockaddr *) &sa, sizeof (struct sockaddr_in)) == SOCKET_ERROR)
     {
      closesocket(s);
      cout << WSAGetLastError();
      return INVALID_SOCKET;
     }
     return s;
    }
    
    int main ()
    {
     WSADATA wsaDat;
     SOCKET test;
    
     if (WSAStartup (MAKELONG (1, 1), &wsaDat) == SOCKET_ERROR)
     {
      cout << "Problem!!!";
      return -1;
     }
    
     if (call_socket ("webpages.atlanticbb.net", 50000) == INVALID_SOCKET)
     {
      cout << "That failed!!!";
      system ("pause");
      return -1;
     }
     return 0;
    }
    Most of the code i got from a tutorial site, but for some reason i cannot connect to the requested server. Can anyone tell me what im doing wrong?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    for some reason i cannot connect
    could you elaborate a little more about that "some" reason.
    What function has failed and what error code you get?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    If you are using windows, dont use teh winsock functions, use HttpSendRequest() and its ilk.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    "... for some reason i cannot connect..."

    Maybe because by default web servers are not listening on port 50000?

    Code:
    call_socket ("webpages.atlanticbb.net",80)
    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Persistent HTTP Requests
    By reelbigtim in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-08-2008, 12:06 AM
  2. Timing HTTP GET requests
    By traef06 in forum C Programming
    Replies: 9
    Last Post: 09-08-2008, 09:33 PM
  3. viewing HTTP requests
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-09-2007, 06:44 PM
  4. Writing all HTTP requests from a website to a log file
    By goomyman in forum C# Programming
    Replies: 1
    Last Post: 07-29-2005, 09:18 AM