2016-05-02 10 views
0

私は私は私のプログラム私はウェブサイトからいくつかの時間ではなくデータを取得するいくつかの時間を実行すると、C、 でシンプルなHTTPクライアントを作成しようとしています。特定のrecv()ブロック、および接続シャットダウンする 。この問題に対処するための 何最善の方法は?私はまだウェブサイトのデータを取得したい。シンプルなHTTPクライアントC用のrecv()ブロック

ZeroMemory(&hints, sizeof(hints)); 
hints.ai_family = AF_UNSPEC; 
hints.ai_socktype = SOCK_STREAM; 
hints.ai_protocol = IPPROTO_TCP; 
// Resolve the server address and port 
if (iResult = getaddrinfo(s1, "http", &hints, &result)) { 
    printf("getaddrinfo failed with error: %d\n", iResult); 
    WSACleanup(); 
    return 1; 
} 
// Attempt to connect to an address until one succeeds 

for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) { 

    // Create a SOCKET for connecting to server 
    ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, 
     ptr->ai_protocol); 
    if (ConnectSocket == INVALID_SOCKET) { 
     printf("socket failed with error: %ld\n", WSAGetLastError()); 
     WSACleanup(); 
     return 1; 
    } 


    iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen); 
    if (iResult == SOCKET_ERROR) { 
     closesocket(ConnectSocket); 
     ConnectSocket = INVALID_SOCKET; 
     continue; 
    } 
    break; 
} 

freeaddrinfo(result); 

if (ConnectSocket == INVALID_SOCKET) { 
    printf("Unable to connect to server!\n"); 
    WSACleanup(); 
    return 1; 
} 


// Send an initial buffer 
iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0); 
if (iResult == SOCKET_ERROR) { 
    printf("send failed with error: %d\n", WSAGetLastError()); 
    closesocket(ConnectSocket); 
    WSACleanup(); 
    return 1; 
} 

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

// Receive until the peer closes the connection 

do {   
    iResult = recv(ConnectSocket, recvbuf, recvbuflen-1, 0); 
    if (iResult > 0) { 
     flag = 1; 
     recvbuf[iResult] = '\0'; 
     printf("%s\n",recvbuf); 
    } 
    else if (iResult == 0) { 
      if(!flag) { 
       printf("conection close before recv data\n"); 
      } else { 
       printf("conection close\n"); 
       break; 
      } 
    } 
    else { 
     printf("recv failed with error: %d\n", WSAGetLastError()); 
    } 
} while(iResult>1); 


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

return 0; 

} iはHTTPプロトコルの概念を使用し、私はこのテンプレットに試みた各URLに送信されてきた

:sendbuf = "GET/[URLパス] HTTP 1.0/R/nHost:[WWW [DaSourcererのアドバイス]をHTTP/1.1に変更してConnectionを追加しました:close、close、まだいくつかの時間はデータを取得していません。 。

も私が追加:

int tcp_timeout=10000; 
unsigned int sz = sizeof(tcp_timeout); 
setsockopt(ConnectSocket, SOL_SOCKET, SO_RCVTIMEO, 
(char*)&tcp_timeout, sz); 

このアイデアは助けにはなりませんでした。

+0

ポストピース。それは速い応答を得るのを助けるでしょう –

答えて

0

私はあなたのタイムアウトが厳しすぎると思います。あなたの送られたヘッダーは、さらに入力を待つためにサーバーを誘発します。これを試してみてください:

GET /path HTTP/1.1 
Host: example.com 
Connection: close 

またHostは、HTTP/1.1で導入されたヘッダであることを言及負います。 HTTP/1.0では意味がありません。あなたはほぼ確実にHTTP/1.1でチャンクエンコードされたメッセージに遭遇しようとしているとして、あなたはまた、RFC 7230, section 3.3.3に興味がある可能性があり。しかし、心配しないでください:単純な状態マシンで解析することができます。代わりに、いくつかのリンクを与えるのコードの

関連する問題