SFML community forums

Help => Network => Topic started by: Marukyu on February 24, 2012, 07:04:29 pm

Title: How to deal with non-blocking TcpSocket returning NotReady?
Post by: Marukyu on February 24, 2012, 07:04:29 pm
Hello,

right now, I am writing the networking code for my project and trying to figure out how to work with a non-blocking TCP socket for the client. Whenever I try to connect to my debug server, TcpSocket::Connect() returns Socket::NotReady, but the server can already see the connection and both server and client can send/receive packets. If I call TcpSocket::Connect() again, it returns Socket::Done. The different return values for the different socket functions don't seem to be too well documented, and checking the Unix implementation source code didn't help me much either since there seem to be various causes for Socket::NotReady.

I am using Crunchbang Linux and a quite recent SFML 2.0 (shortly after the time API change was committed), here is some example code that should reproduce the issue, provided it can connect to any TCP server running at the specified IP and port.

Code: [Select]
int main()
{
    sf::TcpSocket NetTCP;
    NetTCP.SetBlocking(false);
    switch (NetTCP.Connect("localhost", 8989, sf::Time::Zero))
    {
    case sf::Socket::Done:
        std::cout << "Connection successful" << std::endl;
        break;
    case sf::Socket::Disconnected:
        std::cout << "Connection refused" << std::endl;
        break;
    case sf::Socket::Error:
        std::cout << "Socket error" << std::endl;
        break;
    case sf::Socket::NotReady:
        std::cout << "Socket not ready" << std::endl;
        break;
    default:
        std::cout << "Unknown Error" << std::endl;
        break;
    }
}


Commenting out the "SetBlocking(false)" line prints "Connection successful". Changing the timeout does not seem to have any effect.
Should I just ignore the return value of Connect() if it's NotReady? If this is just a way for the socket to say, "I am connecting, wait until I am ready", I would like to know how to poll the socket status during that time, since sockets don't appear to have any kind of "IsReady()" function.
I tried searching the forums, and found someone who had a similar issue, but apparently it was related to the fact that they were using a SocketSelector, while this client-side code is only for a single TCP socket.

As always, any help or support is greatly appreciated. ^^
Title: How to deal with non-blocking TcpSocket returning NotReady?
Post by: Laurent on February 24, 2012, 08:30:04 pm
Quote
If this is just a way for the socket to say, "I am connecting, wait until I am ready", I would like to know how to poll the socket status during that time

The same way as you would do with Receive: call the function (Connect) until it returns Done.
Title: How to deal with non-blocking TcpSocket returning NotReady?
Post by: Marukyu on February 24, 2012, 08:32:40 pm
Ok, I'll do that, thanks for clearing this up! :)