套接字应用程序有问题。它必须多次从“ya.ru”检索html起始页,但在第一次之后,我立即收到套接字错误10053。请看代码:
#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() 和 connect() 不会导致错误。防火墙(作为 Windows 防火墙)和 AV 软件不工作。所以输出是:
0, 8386
0, 0
10053, -1
10053, -1
...
它有什么问题以及如何解决?