Solved

connection request

Posted on 2000-04-17
7
262 Views
Last Modified: 2013-11-13
when i connected to the net
and i go in my broweser to www.google.com
my broweser make the next request :
GET http://www.google.com/ HTTP... .. .

I want to know to what ip my brower will connect to send this request???

Is it a static ip or depandable on the site i'm connecting to???

How can i get this ip<in a programic way>???

btw i don't use proxy server


0
Comment
Question by:ntdragon
  • 3
  • 2
  • 2
7 Comments
 

Expert Comment

by:Vile
ID: 2725723
The ip depends on the site you are connecting to. To resolve the ip address from the name string you can use the function gethostbyname() function. There is an example here:
www.lcg.org/sock-faq/detail.php3?id=32

0
 
LVL 8

Accepted Solution

by:
Christoffer Swanström earned 40 total points
ID: 2726467
The HTTP request message looks actually like this:

GET /somedir/page.html HTTP/1.1
....

You don't give the host name in the request, just the directory and file name you want.

The request message is sent to the IP address of the host (for example www.google.com), port 80. As Vile already mentioned, you can use gethostbyname() to translate a host name to an IP address. This works both in UNIX and with WinSock.

You can find quite a nice description of the HTTP protocol at

http://gaia.cs.umass.edu/kurose/apps/http.htm

0
 
LVL 1

Author Comment

by:ntdragon
ID: 2728392
i"ll check the func and then i"ll get back
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 1

Author Comment

by:ntdragon
ID: 2728405
another question before i'm checking the func

if i'm geting a file will the func work then

i mean if my request is something like

GET http://www.goole.com/home_page1.gif HTTP1 ... .. .

will this func retrive the servers ip
to who i should connect to send this request???
0
 

Expert Comment

by:Vile
ID: 2729377
You should strip the address off and run that through gethostbyname() and then set up a sockets connection with that server on port 80 (http) then send it the GET request
so the server ip is resolved by gethostbyname() (ie. www.google.com = XXX.XXX.XXX.XXX)
and then connect to that ip with port 80 and send
GET /home_page1.gif HTTP/1.0
0
 
LVL 8

Expert Comment

by:Christoffer Swanström
ID: 2729704
Here's basically what you would do to get a document from an HTTP server:

-load winsock
-create a socket
-resolve the address (i.e. get the IP address corresponding to the server name)
-connect
-send the HTTP request
-receive the document


#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>

#define DEFAULT_BUFFER      2048
#define DEFAULT_MESSAGE     "GET /index.html HTTP/1.0\n\n\0"

char  szServer[128],                  // Server to connect to
      szMessage[1024];                  // Message to send to sever
int   iPort     = 80;                  // Port on server to connect to


int main(int argc, char **argv)
{
    WSADATA                        wsd;
    SOCKET                        sClient;
    char                        szBuffer[DEFAULT_BUFFER];
    int                              ret, i;
    struct sockaddr_in      server;
    struct hostent            *host = NULL;
      
      
      // load winsock

    if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
    {
        printf("Failed to load Winsock library!\n");
        return 1;
    }

    strcpy(szMessage, DEFAULT_MESSAGE);

    //
    // Create the socket, and attempt to connect to the server
    //
    sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sClient == INVALID_SOCKET)
    {
        printf("socket() failed: %d\n", WSAGetLastError());
        return 1;
    }
    server.sin_family = AF_INET;
    server.sin_port = htons(iPort);

      
      strcpy(szServer, "www.google.com");

    host = gethostbyname(szServer);
    if (host == NULL)
    {
        printf("Unable to resolve server: %s\n", szServer);
        return 1;
    }
    CopyMemory(&server.sin_addr, host->h_addr_list[0], host->h_length);

    if (connect(sClient, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
    {
        printf("connect() failed: %d\n", WSAGetLastError());
        return 1;
    }

    // Send and receive data
    //

    ret = send(sClient, szMessage, strlen(szMessage), 0);
      
      if (ret == SOCKET_ERROR)
        printf("send() failed: %d\n", WSAGetLastError());

      while(1){
            ret = recv(sClient, szBuffer, DEFAULT_BUFFER, 0);
        if (ret == 0)        // Graceful close
            break;
        else if (ret == SOCKET_ERROR)
        {
            printf("recv() failed: %d\n", WSAGetLastError());
            break;
        }

        printf("RECV [%d bytes]: '%s'\n", ret, szBuffer);

      }


      closesocket(sClient);

    WSACleanup();
    return 0;
}
0
 
LVL 1

Author Comment

by:ntdragon
ID: 2730460
Think you all
tosse you helped me alot
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question