Solved

connection request

Posted on 2000-04-17
7
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

735 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