C++ winsock连接方法上出现错误10038
我正在尝试设置一个与服务器应用程序的简单连接,该服务器应用程序与客户端运行在同一台计算机上 我的代码如下所示:C++ winsock连接方法上出现错误10038,c++,winsock,wsastartup,C++,Winsock,Wsastartup,我正在尝试设置一个与服务器应用程序的简单连接,该服务器应用程序与客户端运行在同一台计算机上 我的代码如下所示: void Base::Connect(string ip, string port) { int status; SOCKET ConnectSocket = INVALID_SOCKET; struct addrinfo hints; struct addrinfo *servinfo; // will point to the results
void Base::Connect(string ip, string port)
{
int status;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo hints;
struct addrinfo *servinfo; // will point to the results
memset(&hints, 0, sizeof hints); // make sure the struct is empty
hints.ai_family = AF_UNSPEC; // don't care IPv4 or IPv6
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
// get ready to connect
status = getaddrinfo(ip.c_str(), port.c_str(), &hints, &servinfo);
// Socket Setup
if (ConnectSocket = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol) == INVALID_SOCKET)
{
printf("[NETWORKING] An error occured when setting up socket\n");
}
// Connect
if (connect(ConnectSocket, servinfo->ai_addr, (int)servinfo->ai_addrlen) == SOCKET_ERROR)
{
int error = WSAGetLastError();
printf("Connect error: ", error);
}
}
之前,我调用了WSAStartup()
,它不会抛出任何错误。如果服务器处于打开或关闭状态,则错误不会更改
我使用的IP是127.0.0.1,通过端口80连接。我试过别的东西(1337年),它给了我同样的错误
有什么明显的问题吗?对可能出现的问题有什么想法吗
if (ConnectSocket = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol) == INVALID_SOCKET)
您正在将套接字(…)与无效的\u套接字进行比较
然后将结果true/false分配给ConnectSocket。
使用
<>参见C++运算符优先级< /p> WSANEOTSOCK的列表。您的套接字创建中存在括号错误。您在哪里看到括号错误?我找不到解决它的实际socket()方法有任何错误(等待7分钟将其标记为正确答案),但现在它抛出10061(被…主动拒绝)。顺便说一句,我让服务器运行。WSAECONNREFUSED:通常是以下情况之一:a)指定端口没有服务器程序。b) 防火墙设置。我关闭了防火墙,但它仍然返回10061错误,端口相同(80)。我将进一步研究它,稍后再提出一个新问题。感谢您的帮助WSAECONNREFUSED
也可能意味着服务器的待办事项中有太多待处理的客户端,此时无法接受新连接。
if ((ConnectSocket = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) == INVALID_SOCKET)