c++ - http client blocks on recv() -


I need some help writing an HTTP client trouble comes when I try to get data from a website Recv () call block program. Any better direction would be very useful, I would post my code below:

  if (argc! = 2) {cerr & lt; & Lt; "Use:" & lt; & Lt; Argv [0]; Serie Lieutenant; & Lt; "& Lt; URI & gt;" & Lt; & Lt; Endl; Return 1; } And {uri_string = argv [1]; } // Create a URI object and parse it URI_STRING URI * YURY = URI: Pars (Yuri_String); If (Yuri == tap) {cerr & lt; & Lt; "Error: URI can not be parsed." & Lt; & Lt; Endl; Return 2; } // Check the specified port number, if you do not use a port 80 unsigned ports = 80; If (uri-> Is_port_defined ()) {port = uri- & gt; Gate_port (); } // Create a TCP socket and connect to the server int tcp_sock = socket (AF_INET, SOCK_STREAM, 0); If (tcp_sock & lt; 0) {cerr & lt; & Lt; "Unable to create TCP socket." & Lt; & Lt; Endl; Return 3; } Sockaddr_in Server; Socklen_t slen = sizeof (server); Server.can_family = AF_INET; Server.sin_port = htons (port); Hostname * hostp = gethostbyname (Yuri-> Get_host () .c_str ()); Memcpy (& server.cn_addr, hostp- & gt; H_ADD, hostP-> H_Length); If (connected (TCP_sock, (Socadar *) and server, Slane) <0] {cerr & lt; & Lt; "Unable to connect to server via TCP." & Lt; & Lt; Endl; Off (TCP_sock); Return 4; } // Server HTTP_Request * Request = HTTP_Request :: Create_GET_request (URI-> Get_path ()) to create an HTTP request; Request-> Set_host (yur- gt; gate_host ()); String request_string = ""; Request-> Print (request_string); // cout & lt; & Lt; Request_string & lt; & Lt; Endl; // Send it to the server, wait for the answer and use HTTP_Response to send the reply (TCP_ask, and request_string, size (request_string), 0); Char recv_buffer [1024]; Int bytes_recv = 0; While (bytes_recv <1024) {int recv_len = recv (tcp_sock, recv_buffer + bytes_recv, 1024 - byte_requisy, 0); If (recv_len == -1) {cerr & lt; & Lt; "Error getting response from server." & Lt; & Lt; Endl; Off (TCP_sock); Return 5; } Bytes_recv + = recv_len; } HTTP_Response * response = HTTP_Response :: Parse (recv_buffer, bytes_recv); String response_string = ""; Feedback-> print (response_string); Cout & lt; & Lt; Response_string & lt; & Lt; Endl; Return 0;  

}

You can use blocking TCP / IP socket , But you are not seeing how many bytes are reading for the HTTP response's "content-length" headers to see. Your current reading logic is calling RICV () in the loop until maximum 1024 bytes are received. If the server sends less than 1024 bytes, then you are going to be blocked indefinitely because you are saying recv () many times to beets sometimes asked for bytes.


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -