编辑:搞砸了我对接受调用的伪编码,它现在反映了我实际在做什么。
我有两个插座。我正在尝试在两者之间使用发送/接收。当监听套接字阻塞时,它可以看到连接并接收它。当它是非阻塞的时,我进行了一个忙碌的等待(只是为了调试它)并且它超时,总是带有 error EWOULDBLOCK
。为什么侦听套接字无法看到阻塞时可以看到的连接?
代码主要在函数中分离,但这里有一些我正在做的伪代码。
int listener = -2;
int connector = -2;
int acceptedSocket = -2;
getaddrinfo(port 27015, AI_PASSIVE) results loop for listener socket
{
if (listener socket() == 0)
{
if (listener bind() == 0)
if (listener listen() == 0)
break;
listener close(); //if unsuccessful
}
}
SetBlocking(listener, false);
getaddrinfo("localhost", port 27015) results loop for connector socket
{
if (connector socket() == 0)
{
if (connector connect() == 0)
break; //if connect successful
connector close(); //if unsuccessful
}
}
loop for 1 second
{
acceptedSocket = listener accept();
if (acceptedSocket > 0)
break; //if successful
}
This just outputs a huge list errno of EWOULDBLOCK
before ultimately ending the timeout loop. If I output the file descriptor for the accepted socket in each loop interation, it is never assigned a file descriptor.
The code for SetBlocking is as so:
int SetBlocking(int sockfd, bool blocking)
{
int nonblock = !blocking;
return ioctl(sockfd,
FIONBIO,
reinterpret_cast<int>(&nonblock));
}
If I use a blocking socket, either by calling SetBlocking(listener, true)
or removing the SetBlocking()
call altogether, the connection works no problem.
Also, note that this connection with the same implementation works in Windows, Linux, and Solaris.