Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++中在Winsock中发送HTTP GET请求后的函数挂起_C++_Http_Post_Winsock_Recv - Fatal编程技术网

C++中在Winsock中发送HTTP GET请求后的函数挂起

C++中在Winsock中发送HTTP GET请求后的函数挂起,c++,http,post,winsock,recv,C++,Http,Post,Winsock,Recv,我试图在winsock中创建一个使用HTTP的程序,但是遇到了一个问题,recv函数就挂在那里 int connect() { WSADATA t_wsa; //WSADATA structure WORD wVers = 0x0202; //version number int iError; //error number wVers = MAKEWORD(2, 2); // Set the version number to 2.2 iError = WSAStartup(wVers,

我试图在winsock中创建一个使用HTTP的程序,但是遇到了一个问题,recv函数就挂在那里

int connect() 
{
WSADATA t_wsa; //WSADATA structure
WORD wVers = 0x0202; //version number
int iError; //error number

wVers = MAKEWORD(2, 2); // Set the version number to 2.2
iError = WSAStartup(wVers, &t_wsa); // Start the WSADATA

if(iError != NO_ERROR || iError == 1)
{
    printf("Error at WSAStartup()\n");
    WSACleanup();
    system("PAUSE");
    return 1;
}

/* Correct version? */
if(LOBYTE(t_wsa.wVersion) != 2 || HIBYTE(t_wsa.wVersion) != 2)
{
    printf("Incorrect version\n");
    WSACleanup();
    system("PAUSE");
    return 1;
}

SOCKET sClient;
sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sClient == INVALID_SOCKET || iError == 1)
{
    printf("Invalid Socket!\n");
    WSACleanup();
    system("PAUSE");
    return 1;
}
SOCKADDR_IN sinClient;
memset(&sinClient, 0, sizeof(sinClient));

char cIP[50];
strcpy_s(cIP, "98.139.183.24");
sinClient.sin_family = AF_INET;
sinClient.sin_addr.s_addr = inet_addr(cIP); // Where to start server
sinClient.sin_port = htons(80); //Port

if(connect(sClient, (LPSOCKADDR)&sinClient, sizeof(sinClient)) == SOCKET_ERROR)
{
    /* failed at starting server */
    printf("Could not connect ot the server!\n");
    WSACleanup();
    system("PAUSE");
    return 1;
}   

// Now we can send/recv data!
printf("YOU ARE CONNECTED!\r\n");
string buffer;
buffer += "GET / HTTP/1.1\r\n";
buffer += "Host: http://www.yahoo.com/\r\n";
buffer += "Connection: close\r\n\r\n";
const char *cha = buffer.c_str();


int sent;
int response;
sent = send(sClient, cha, sizeof(cha) - 1, 0);

char recvbuf[50000];

response = recv(sClient, recvbuf, 50000, 0);
recvbuf[response] = '\0';
printf("\nReceived data = %s", recvbuf);
WSACleanup();
return(0);
}
send将在send函数后打印,但recv打印后不会打印任何内容


我在这里遗漏了什么?

一个可能的原因是发送未发送预期的数据:

sent = send(sClient, cha, sizeof(cha) - 1, 0);
sizeofcha-1实际上是sizeofchar*-1,而不是数据的实际长度:请改用buffer.length

请注意,您可以在一条语句中使用字符串文字构造std::string,而不是通过几个缩合来构造它。但是,由于std::string用于获取常量字符*,因此完全没有理由使用std::string:

检查send和recv的返回值以确定成功或失败,特别是当结果用于索引数组时,检查recv。失败时,recv返回SOCKET_错误,我认为是-1


正确处理HTTP响应需要大量的工作。接收代码需要检查返回的响应,以确定如何处理响应内容。例如,HTTP响应可能是分块的,也可能不是。存在用于管理HTTP请求的库,其中一个是在大约2013年2月宣布的。

一个可能的原因是发送未发送预期的数据:

sent = send(sClient, cha, sizeof(cha) - 1, 0);
sizeofcha-1实际上是sizeofchar*-1,而不是数据的实际长度:请改用buffer.length

请注意,您可以在一条语句中使用字符串文字构造std::string,而不是通过几个缩合来构造它。但是,由于std::string用于获取常量字符*,因此完全没有理由使用std::string:

检查send和recv的返回值以确定成功或失败,特别是当结果用于索引数组时,检查recv。失败时,recv返回SOCKET_错误,我认为是-1


正确处理HTTP响应需要大量的工作。接收代码需要检查返回的响应,以确定如何处理响应内容。例如,HTTP响应可能是分块的,也可能不是。存在用于管理HTTP请求的库,其中一个库大约于2013年2月发布。

由于代码使用WinSock,它在Windows上运行,因此可以使用Microsoft的WinInet或WinHTTP API作为直接使用WinSock的替代方法。在linux环境中使用该函数时,我遇到了同样的问题。我使用了sizeof,这使得recv功能变得简单。现在,我正在使用strlen,它正在按预期工作。由于代码使用WinSock,它在Windows上运行,因此可以使用Microsoft的WinInet或WinHTTP API作为直接使用WinSock的替代方法。在linux环境中使用该函数时,我遇到了同样的问题。我使用了sizeof,这使得recv功能变得简单。现在我正在使用strlen,它正在按预期工作。