简体   繁体   English

如何使用C获取站点IP地址

[英]How to get site ip address using c

I am trying to build a HTTP request using sockets in c. 我正在尝试使用c中的套接字建立一个HTTP请求。 So, in order to navigate the socket to the correct site ip. 因此,为了将套接字导航到正确的站点ip。 I need to get the site ip. 我需要获取站点ip。 I have managed to get the host ip but that not always work. 我已经设法获得了主机IP,但并非总是能正常工作。 The following code gets the host ip: 以下代码获取主机ip:

host = gethostbyname(host_name);
if (host != NULL) {
    memcpy(&inp, host->h_addr_list[0], host->h_length);
    sprintf(ip, "%s", inet_ntoa(inp));
}

But that not always work, for example if I want to send the socket to stackoverflow.com and get his HTML content.I used this code and the output was: "198.252.206.16". 但这并不总是可行的,例如,如果我想将套接字发送到stackoverflow.com并获取他的HTML内容。我使用此代码,输出为:“ 198.252.206.16”。 And if you enter that ip you can see that it is a wrong ip,so what can I do? 如果输入该IP,您会发现它是错误的IP,那我该怎么办? Please help. 请帮忙。

PS that this all my code: PS这就是我所有的代码:

#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h> 
#include <errno.h>
#include <assert.h>

int main(int argc, char **argv) {

    if (argc < 2) {
        fprintf(stderr, "usage: %s domain_name\nE.g. %s www.yahoo.com/lalal.html\n", argv[0], argv[0]);
        return(0);
    }

struct protoent *pr;
struct in_addr inp;
int x = 1;
int ret;
char buf[4192];
char ip[16];
struct hostent *host;
int sock, bytes_recieved;
struct sockaddr_in server_addr;

char url[strlen(argv[1])];
strcpy(url,argv[1]);
char *index_page = strstr(argv[1], "/");
char *host_name = strtok(url,"/");
char message[4000];
sprintf(message,"GET %s HTTP/1.1\r\nHost: %s\r\n\r\n",index_page,host_name);
printf("%s",message);

host = gethostbyname(host_name);
if (host != NULL) {
    memcpy(&inp, host->h_addr_list[0], host->h_length);
    sprintf(ip, "%s", inet_ntoa(inp));
}
else {
    printf("ERROR - Host ip was not found.\n\n");
    exit(1);
}

printf("%s\n",ip);

pr = getprotobyname("tcp");
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("Socket");
    exit(1);
}
printf("%s\n",message);
server_addr.sin_family = AF_INET;     
server_addr.sin_port = htons(80);   
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8); 

if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
    perror("Connect");
    exit(1);
}

write(sock, message, strlen(message));

while ((ret = read(sock, buf, 4192)) != 0) {
    buf[ret]='\0';
    fwrite(buf, ret, sizeof(char), stdout);
    x++;
}

if (close(sock) == -1)
    printf("Close socket error\n");

return 0;

} }

StackExchange hosts multiple sites on the same server. StackExchange在同一服务器上托管多个站点。 198.252.206.16 is the IP address of that server, and that is the correct IP address you need to connect your socket to. 198.252.206.16是该服务器的IP地址,这是您将套接字连接到的正确IP地址。

When requesting an HTTP resource from a site that resides on a shared server, you must provide an HTTP Host header to specify the site's hostname so the server knows which site you are trying to access. 当从位于共享服务器上的站点请求HTTP资源时,必须提供HTTP Host标头以指定站点的主机名,以便服务器知道您要访问的站点。

For example, if you go to http://198.252.206.16 , the request would look like this: 例如,如果您转到http://198.252.206.16 ,则请求将如下所示:

(connect to 198.252.206.16)

GET / HTTP 1.1
Host: 198.252.206.16
...

If you go to http://www.stackoverflow.com , the request looks like this: 如果您访问http://www.stackoverflow.com ,则请求如下所示:

(connect to 198.252.206.16)

GET / HTTP 1.1
Host: www.stackoverflow.com
...

If you go to http://www.stackexchange.com , the request looks like this: 如果您访问http://www.stackexchange.com ,则请求如下所示:

(connect to 198.252.206.16)

GET / HTTP 1.1
Host: www.stackexchange.com
...

Notice that they all connect to the same IP address. 请注意,它们都连接到相同的IP地址。

There is no site associated with the 198.252.206.16 host, which is why you get the error message. 没有与198.252.206.16主机关联的198.252.206.16 ,这就是为什么您收到错误消息的原因。

The Host header is required for all HTTP 1.1 requests, and is optional for HTTP 1.0 requests (but an HTTP 1.0 request will fail in this situation if the Host header is missing). Host标头对于所有HTTP 1.1请求都是必需的,并且对于HTTP 1.0请求是可选的(但是,如果缺少Host标头,则HTTP 1.0请求在这种情况下将失败)。 It was designed specifically to support multiple sites on a shared server. 它专门用于支持共享服务器上的多个站点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM