WinSock2 发送在 6 次循环迭代后返回 SOCKET_ERROR...为什么?
6 次迭代后,发送返回 -1,即 SOCKET_ERROR。我尝试添加一个睡眠函数,这样它会在再次循环之前等待一秒钟,但这使得它只会迭代两次。是什么导致了这个错误?
#pragma comment(lib, "ws2_32.lib")
#include <iostream>
#include <windows.h>
void main ()
{
int reqVersion = 2;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(reqVersion,0), &wsaData)==0)
{
// Check if major version is at least reqVersion
if (LOBYTE(wsaData.wVersion) >= reqVersion)
{
SOCKADDR_IN addr;
int addrlen = sizeof(addr);
SOCKET sListen;
SOCKET sConnect;
sConnect = socket(AF_INET, SOCK_STREAM, NULL);
addr.sin_addr.s_addr = inet_addr("192.168.0.7");
addr.sin_family = AF_INET;
addr.sin_port = htons(1234);
if(connect(sConnect, (SOCKADDR*)&addr, sizeof(addr)) == 0)
{
char message[10] = "Hellooooo";
for(int i = 0; i <50; i++)
{
int sendOutcome = send(sConnect, message, sizeof(message), 0);
if(sendOutcome == SOCKET_ERROR)
{
std::cout << "Socket Error..." << std::endl;
}
else
{
std::cout << "SENT..." << std::endl;
}
Sleep(1000);
}
}
else
{
std::cout << "Not Connected..." << std::endl;
}
}
else
{
std::cout << "Required version not available..." << std::endl;
}
// Cleanup winsock
if (WSACleanup()!=0)
{
std::cout << "Cleanup failed..." << std::endl;
}
}
else
{
std::cout << "Startup failed..." << std::endl;
}
system("PAUSE");
}
After 6 iterations, the send returns -1 aka SOCKET_ERROR. I tried adding a sleep function so it would wait a second before looping through again, but that made it so that it would only iterate 2 times. What is causing this error?
#pragma comment(lib, "ws2_32.lib")
#include <iostream>
#include <windows.h>
void main ()
{
int reqVersion = 2;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(reqVersion,0), &wsaData)==0)
{
// Check if major version is at least reqVersion
if (LOBYTE(wsaData.wVersion) >= reqVersion)
{
SOCKADDR_IN addr;
int addrlen = sizeof(addr);
SOCKET sListen;
SOCKET sConnect;
sConnect = socket(AF_INET, SOCK_STREAM, NULL);
addr.sin_addr.s_addr = inet_addr("192.168.0.7");
addr.sin_family = AF_INET;
addr.sin_port = htons(1234);
if(connect(sConnect, (SOCKADDR*)&addr, sizeof(addr)) == 0)
{
char message[10] = "Hellooooo";
for(int i = 0; i <50; i++)
{
int sendOutcome = send(sConnect, message, sizeof(message), 0);
if(sendOutcome == SOCKET_ERROR)
{
std::cout << "Socket Error..." << std::endl;
}
else
{
std::cout << "SENT..." << std::endl;
}
Sleep(1000);
}
}
else
{
std::cout << "Not Connected..." << std::endl;
}
}
else
{
std::cout << "Required version not available..." << std::endl;
}
// Cleanup winsock
if (WSACleanup()!=0)
{
std::cout << "Cleanup failed..." << std::endl;
}
}
else
{
std::cout << "Startup failed..." << std::endl;
}
system("PAUSE");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 sarnold 的评论所建议的那样,获取实际错误总是有用的。
一般来说,检查错误的套接字代码将在通过检查
SOCKET_ERROR
返回值来检测问题的代码之后调用WSAGetLastError()
。从
WSAGetLastError()
获得的错误代码很重要,一旦您知道它的含义,您通常就可以推断出问题。您可以使用 < 显示此错误代码的文本code>FormatMessage() 或者您可以在 WinError.h 标头中手动查找它。
在本例中,10053 是
WSAECONNABORTED
,错误文本显示“已建立的连接被主机中的软件中止”。因此,我假设您正在连接的服务器由于某种原因正在中止连接。如果不查看服务器源代码,很难知道为什么会这样。
It's always useful to obtain the actual error, as sarnold's comment suggested.
In general, socket's code that checks for errors will have a call to
WSAGetLastError()
after the code that detects the issue by checking for aSOCKET_ERROR
return value.The error code that you get from
WSAGetLastError()
is important and you can usually reason about the problem once you know what it means.You can display the text of this error code using
FormatMessage()
or you can look it up by hand in the WinError.h header.In this case, 10053 is
WSAECONNABORTED
for which the error text says "An established connection was aborted by the software in your host machine."So, I would assume that the server that you are connecting to is aborting the connection for some reason. Without seeing the server source code it's hard to know why that might be.