[英]Error 10038 on winsock connect method
I'm trying to setup a simple connection with a server app running on the same computer as the client. 我正在尝试与与客户端在同一台计算机上运行的服务器应用程序建立简单连接。
My code looks like this: 我的代码如下所示:
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);
}
}
Beforehand, I call WSAStartup()
and it doesn't throw any errors. 事先,我调用了
WSAStartup()
,它不会引发任何错误。 If the server is on or off the error doesn't change. 如果服务器打开或关闭,错误不会改变。
the IP I use is 127.0.0.1 and I'm connection through port 80. I've tried something else (1337) which gave me the same error. 我使用的IP是127.0.0.1,我通过端口80进行连接。我尝试了其他操作(1337),这给了我同样的错误。
Is there something obviously wrong? 明显有问题吗? Any ideas on what might be going wrong?
关于可能出了什么问题的任何想法?
if (ConnectSocket = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol) == INVALID_SOCKET)
You´re comparing socket(...) to INVALID_SOCKET
您正在将套接字(...)与
INVALID_SOCKET
进行比较
and then you´re assigning the result true/false to ConnectSocket. 然后将结果true / false分配给ConnectSocket。
Use 采用
if ((ConnectSocket = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) == INVALID_SOCKET)
See lists of C++ operator precedence 查看C ++运算符优先级列表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.