Solved

connection request

Posted on 2000-04-17
7
265 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
Technology Partners: 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes

717 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