简体   繁体   中英

C++ WinSock error 10053

Have problems with socket application. It must retrieve html start page from "ya.ru" many times, but immediately after the first time, I get socket error 10053. Please look at code:

#include <stdio.h>
#include <winsock2.h>
int main()
{
WSADATA winsock_data;
WSAStartup (MAKEWORD (2, 0), &winsock_data);

SOCKET socket_body;
socket_body = socket (PF_INET, 1, 0);

struct timeval to;
to.tv_sec = 5000;
to.tv_usec = 0;
setsockopt(socket_body, SOL_SOCKET, SO_RCVTIMEO, (char *)&to, sizeof(struct timeval));

sockaddr_in destaddr;
destaddr.sin_family = PF_INET;
destaddr.sin_port = htons( 80 );
destaddr.sin_addr.s_addr = inet_addr( "93.158.134.3" );
connect ( socket_body, (struct sockaddr*)&destaddr, sizeof(sockaddr) );

char* buf = (char*)malloc(20000);
int n;
while(1)
{
    send(socket_body, "GET / HTTP/1.1\r\nHost: ya.ru\r\n\r\n", 31, 0);
    Sleep(100);
    n = recv(socket_body, buf, 20000, 0);
    printf("%d, %d\n", GetLastError(), n);
    //if (n>0) printf("%s", buf);
    Sleep(1000);
}
}

WSAStartup, socket(), setsockopt() and connect() doesn't cause errors. The firewall (as Windows firewall) and AV software isn't working. So the output is:

0, 8386
0, 0
10053, -1
10053, -1
...

What's wrong with it and how it can be solved?

You are assuming that the connection is staying open after each HTTP response is received, but that is not guaranteed. You have to look at the server's actual HTTP response (specifically, at the HTTP version reported in the status line, and at the Connection header) to know if the server is leaving the connection open or not.

You are sending an HTTP 1.1 request, but there is no guarantee that the server will reply with an HTTP 1.1 response.

If the response is using HTTP 0.9, you must close the socket and reconnect before sending the next HTTP request.

If the response is using HTTP 1.0, and if the Connection header does not say keep-alive , then you must close the socket and reconnect before sending the next HTTP request.

If the response is using HTTP 1.1, and if the Connection header does not say close , then the server left the connection open on its end, and you can reuse the same connection for the next request. However, you do still need to take into account the possibility that the connection might be closed externally (by a firewall/router/proxy, etc) before you actually send the next HTTP request.

So in the case of either HTTP 1.0 or 1.1, if you are expecting the connection to remain open, but send() fails with an error indicating the connection was lost, simply close the socket and reconnect and then send the same HTTP request again.

Ok, so from this page 10053 is the following:

10053 WSAECONNABORTED

Software caused connection abort. An established connection was aborted by the software in your host computer, possibly due to a data transmission time-out or protocol error.

What do you know about the machine on the other end? Might it be closing the connection? I'm not sure if that would produce this error.

Have you tried closing and reestablishing the connection in the loop?

while(1)
{
  connect ( socket_body, (struct sockaddr*)&destaddr, sizeof(sockaddr) );
  send(socket_body, "GET / HTTP/1.1\r\nHost: ya.ru\r\n\r\n", 31, 0);
  S leep(100);
  n = recv(socket_body, buf, 20000, 0);
  printf("%d, %d\n", GetLastError(), n);
  close(socket_body);
  //if (n>0) printf("%s", buf);
  Sleep(1000);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM