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

Author Topic: Non-Blocking TCP Connect() - How to tell when it has connect  (Read 2147 times)

0 Members and 1 Guest are viewing this topic.

dirk103

  • Newbie
  • *
  • Posts: 3
    • View Profile
Non-Blocking TCP Connect() - How to tell when it has connect
« on: April 30, 2011, 06:37:11 am »
Hey there!
I'd like my application not to freeze during a connect. How can I check to see if my application has made a successful connection?

If I:

while( ! socket->Connect(port, host, 0.0f) )
    cout << ".";

That seems to work, but that seems a little messy. Is there a nice interface to find out error codes for sockets that I can't find in the ref, or is this feature unavailable?

Thanks
Also. SFML is awesome, why was I using SDL before???

dirk103

  • Newbie
  • *
  • Posts: 3
    • View Profile
got it!
« Reply #1 on: April 30, 2011, 07:20:37 am »
Okay, well I had to do a bit of digging, and I found out about the SocketHelper bit. Then I looked at the source and found Socket::Status. I'm just worried, in the documentation (sf::SocketHelper) it says:

"This class is meant for internal use only"  

Now in my method, I'm going to recv and wait until my status is anything but NotReady.

Is this reliable you think?
Thanks a bunch, oh and here's a teaser:

Code: [Select]

sf::Socket::Status ret;

sf::IPAddress host(IP);

/* Check to see if the supplied IP is valid */
if(!host.IsValid()) {
m_error = CERR_INVALID_HOST;
return false;
}

/* IP Looks good, so go ahead and connect */
//m_connection->SetBlocking(false);

ret = m_connection->Connect(port, host, 2.0f);
switch(ret) {
case sf::Socket::Error:
m_error = CERR_CONNECTION_TIMEOUT;
m_state = CLIENT_DISCONNECTED;
return false;
break;

case sf::Socket::Done:
m_error = CERR_OK;
m_state = CLIENT_CONNECTED;
return true;
break;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Non-Blocking TCP Connect() - How to tell when it has connect
« Reply #2 on: April 30, 2011, 09:50:28 am »
Quote
Okay, well I had to do a bit of digging, and I found out about the SocketHelper bit. Then I looked at the source and found Socket::Status. I'm just worried, in the documentation (sf::SocketHelper) it says:

"This class is meant for internal use only"

It's true, you don't have to worry about sf::SocketHelper. The sf::SocketStatus enum can be found in the doc by simply looking at the return type of sf::SocketTCP::Connect.

Quote
Now in my method, I'm going to recv and wait until my status is anything but NotReady.

Receive? Isn't this topic about connecting?
The NotReady status can only be returned if your socket is in non-blocking mode.
Laurent Gomila - SFML developer

 

anything