Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to deal with non-blocking TcpSocket returning NotReady?  (Read 5155 times)

0 Members and 1 Guest are viewing this topic.

Marukyu

  • Newbie
  • *
  • Posts: 36
    • View Profile
How to deal with non-blocking TcpSocket returning NotReady?
« 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. ^^

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
How to deal with non-blocking TcpSocket returning NotReady?
« Reply #1 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.
Laurent Gomila - SFML developer

Marukyu

  • Newbie
  • *
  • Posts: 36
    • View Profile
How to deal with non-blocking TcpSocket returning NotReady?
« Reply #2 on: February 24, 2012, 08:32:40 pm »
Ok, I'll do that, thanks for clearing this up! :)