2

我确定我的程序已经通过 url 从服务器获取了 ip。当我发送 GET 请求时,虽然发送成功,但无法得到响应。我不确定服务器是否收到了我的请求,我应该如何检查服务器是否收到了我的请求?发送请求并等待一段时间后连接将关闭。“recv”函数将响应 0,缓冲区一无所获。

我想知道我的“sendbuf = GET / HTTP/1.1\r\n\r\n”是否有任何错误。我应该使用 1.1 还是 1.0?

char *sendbuf = "GET / HTTP/1.1\r\n\r\n";

//to get the ip from DNS server
pHostEnt = gethostbyname( "www.google.com.tw");
ppaddr = (int**)pHostEnt->h_addr_list;
sockAddr.sin_addr.s_addr = **ppaddr;
printf("%s",inet_ntoa(sockAddr.sin_addr) );


//to create the socket and connect to it
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = inet_addr(inet_ntoa(sockAddr.sin_addr));
serverAddress.sin_port = htons(SERVER_PORT);

serverSocket = socket(AF_INET, SOCK_STREAM, 0);
connect(serverSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress));

//send request
int iResult = send( serverSocket, sendbuf, (int)strlen(sendbuf), 0 );
//get response    
if (bytesRead=recv(serverSocket, buf, MAX_SIZE, 0) < 0)
    printf("Error with send()");
else
    printf("Successfully sent html fetch response");
4

1 回答 1

0

使用 HTTP 1.1 很好,但是您的请求是不完整的,因为 HTTP 1.1Host至少需要标头(以便服务器支持虚拟主机)。

更重要的是,您没有进行任何错误处理。你需要这样做。

您还没有考虑到gethostname()可以返回多个 IP 地址。您需要访问connect()每个地址,直到一个成功,或者直到您用完列表。

尝试这个:

char *sendbuf = "GET / HTTP/1.1\r\nHost: www.google.com.tw\r\n\r\n";

//to get the ip from DNS server
pHostEnt = gethostbyname( "www.google.com.tw"); // you should be using getaddrinfo() instead!
if (!pHostEnd)
{
    printf("Unable to resolve www.google.com.tw\n");
    return; // or whatever...
}

if (pHostEnt->h_addrtype != AF_INET)
{
    printf("www.google.com.tw did not resolve to an IPv4 IP address\n");
    return; // or whatever...
}

serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == INVALID_SOCKET)
{
    printf("Unable to create socket\n");
    return; // or whatever...
}

in_addr **ppaddr = (in_addr**) pHostEnt->h_addr_list;

sockaddr_in serverAddress = {0};
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(SERVER_PORT);

bool connected = false;

int i = 0;
while (ppadd[i] != NULL)
{
    // connect to server

    serverAddress.sin_addr = *(ppaddr[i]);
    printf("Connecting to %s\n", inet_ntoa(serverAddress.sin_addr);

    if (connect(serverSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) == 0)
    {
        connected = true;
        break;
    }

    if (WSAGetLastError() == WSAEWOULDBLOCK)
    {
        fd_set writefds;
        FD_ZERO(&writefds);
        FD_SET(serverSocket, &writefds);

        timeval timeout;
        timeout.tv_sec = 5;
        timeout.tv_usec = 0;

        if (select(0, NULL, &writefds, NULL, &timeout) > 0)
        {
            connected = true;
            break;
        }
    }

    ++i;
}

if (!connected)
{
    printf("Unable to connect to www.google.com.tw\n");
    return; // or whatever...
}

printf("Connected, sending request\n");

//send request
char *ptr = sendbuf;
int len = strlen(sendbuf);

do
{
    int bytesSent = send( serverSocket, ptr, len, 0 );
    if (bytesSent > 0)
    {
        ptr += bytesSent;
        len -= bytesSent;
        continue;
    }

    if (WSAGetLastError() == WSAEWOULDBLOCK)
    {
        fd_set writefds;
        FD_ZERO(&writefds);
        FD_SET(serverSocket, &writefds);

        timeval timeout;
        timeout.tv_sec = 15;
        timeout.tv_usec = 0;

        if (select(0, NULL, &writefds, NULL, &timeout) > 0)
            continue;
    }

    printf("Unable to send request\n");
    return; // or whatever...
}
while (len > 0);

printf("Reading response\n");

//get response    
do
{
    int bytesRead = recv(serverSocket, buf, MAX_SIZE, 0);
    if (bytesRead > 0)
    {
        // process buf as needed...
        // make sure you are paying attention to the
        // Content-Length and Transfer-Encoding headers,
        // as they tell you how to read the response.
        // Refer to RFC 2616 Section 4.4 for details...

        contine;
    }

    if (bytesRead == 0)
    {
        printf("Disconnected");
        return; // or whatever...
    }

    if (WSAGetLastError() == WSAEWOULDBLOCK)
    {
        fd_set readfds;
        FD_ZERO(&readfds);
        FD_SET(serverSocket, &readfds);

        timeval timeout;
        timeout.tv_sec = 15;
        timeout.tv_usec = 0;

        if (select(0, &readfds, NULL, NULL, &timeout) > 0)
            continue;
    }

    printf("Unable to read response\n");
    return; // or whatever...
}
while (true);

closesocket(serverSocket);
于 2012-11-29T00:40:46.733 回答