gpt4 book ai didi

http - winsock2收不到http服务器的响应包

转载 作者:可可西里 更新时间:2023-11-01 16:44:12 25 4
gpt4 key购买 nike

我确定我的程序已经通过 url 从服务器获取了 ip。当我发送GET请求时,虽然发送成功了,但是却得不到响应。我不确定服务器是否收到我的请求,我应该如何检查服务器是否收到我的请求?发送请求并等待一段时间后,连接将关闭。 “recv”函数将响应 0,缓冲区什么也得不到。

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

最佳答案

使用 HTTP 1.1 没问题,但您的请求不完整,因为 HTTP 1.1 至少需要 Host header (以便服务器支持虚拟主机)。

更重要的是,您没有进行任何错误处理。你需要这样做。

您也没有考虑到 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);

关于http - winsock2收不到http服务器的响应包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13609671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com