0
我使用的代碼來自MSDN's recv() page,但我更改了要發送的數據以及目標端口和IP地址,以發送HTTP GET請求以獲取google.com/index .PHP。每次運行它時,recv()在獲得大部分頁面後返回0,但不是全部。我用wireshark驗證整個頁面已收到,但它在<a href=//google.co
之後停止,後面跟着一個非ASCII符號。C WINAPI recv()在收到所有數據之前返回0
下面是我使用的代碼,我拿出大部分的意見和錯誤檢查,但否則它與上面相同的鏈接:
#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;
}
我編譯的Linux上4.2的mingw32版本。 1。
Linux上的'WSACleanup()'? MSDN可能不是在Linux上學習Berkley套接字的最佳位置。嘗試http://beej.us/guide/bgnet –
是什麼讓你認爲你可以在套接字的數據上使用'printf''%s'?它不是零終止的。 – asveikau
我正在與Win32編譯器MingW交叉編譯。 printf%512s應該在到達緩衝區末尾之前停止。我知道它會在最後一次打印垃圾,但我正在尋找關閉