C WINAPI recv()在收到所有数据之前返回0

时间:2011-12-11 06:13:28

标签: c winapi sockets winsock recv

我正在使用MSDN's recv() page中的代码,但我更改了正在发送的数据以及目标端口和IP地址,以发送HTTP GET请求以获取google.com/index.php。每次运行它时,recv()在获取大部分页面后返回0,但不是全部。我使用wireshark验证了整个页面已收到,但它在<a href=//google.co后停止,后跟非ASCII符号。

这是我正在使用的代码,我取出了大部分注释和错误检查,但是它与上面的链接相同:

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

int main() {
    WSADATA wsaData;
    int iResult;

    SOCKET ConnectSocket = INVALID_SOCKET;
    struct sockaddr_in clientService; 

    char *sendbuf = "GET /index.php\r\nHost: www.google.com\r\n\r\n";
    char recvbuf[512];
    int recvbuflen = 512;

    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr( "74.125.224.180" );
    clientService.sin_port = htons( 80 );

    iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );

    iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );

    printf("Bytes Sent: %ld\n", iResult);

    // shutdown the connection since no more data will be sent
    iResult = shutdown(ConnectSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf("shutdown failed: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    // Receive until the peer closes the connection
    do {

        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
        if ( iResult > 0 ){
            printf("%512s", recvbuf);
            //printf("recv returned %d... got %d bytes\n", iResult, recvbuflen);
        }
        else if ( iResult == 0 )
            printf("\n\nConnection closed\n");
        else
            printf("\n\nrecv failed: %d\n", WSAGetLastError());

    } while( iResult > 0 );

    // cleanup
    closesocket(ConnectSocket);
    WSACleanup();

    return 0;
}

我正在用mingw32版本4.2.1在linux上编译。

1 个答案:

答案 0 :(得分:0)

我只是瞥了一眼,但最明显的错误是:

    if ( iResult > 0 ){
        printf("%512s", recvbuf);

没有人会为你写那个使C字符串有效的NUL角色。特别是,由于打印字符串意味着搜索NUL字符,并且没有通过网络发送,因此printf之后的最后一个recv也可能会吐出缓冲区中的一些垃圾。前一个循环迭代。你可以尝试这样的事情:

if (iResult > 0)
{
   char *p = recvbuf;
   while (iResult--)
      fputc(*p++, stdout);
}

这样您只能打印recv告诉您在缓冲区中的字符。