2012-11-28 92 views
2

我確定我的程序已經通過url從服務器獲得了ip。 當我發送GET請求時,雖然它已成功發送,但無法獲得響應。我不確定服務器是否已收到我的請求,我應該如何檢查服務器是否收到我的請求? 連接將在發送請求並等待一段時間後關閉。 「recv」函數將響應0,並且緩衝區什麼都不收。無法從http服務器獲取winsock2的響應包

我不知道我的「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"); 

回答

0

使用HTTP 1.1是好的,但你的請求是不完整的作爲HTTP 1.1需要Host頭,至少是(爲了服務器來支持虛擬主機)。

更重要的是,你沒有做任何錯誤處理。你需要這樣做。

您也沒有考慮到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); 
+0

謝謝你給了我新的方向調試。但發送仍然返回-1。(發送錯誤)我將繼續嘗試解決這些問題。 – MaryHo

+0

當send()返回-1時返回什麼'WSAGetLastError()? –