Solved

connection request

Posted on 2000-04-17
7
260 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
Comment Utility
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
Comment Utility
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
Comment Utility
i"ll check the func and then i"ll get back
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:ntdragon
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Think you all
tosse you helped me alot
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now