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

Author Topic: Issue with non-blocking socket and getRemoteAddress()  (Read 1788 times)

0 Members and 1 Guest are viewing this topic.

Romz

  • Newbie
  • *
  • Posts: 4
    • View Profile
Issue with non-blocking socket and getRemoteAddress()
« on: August 19, 2013, 02:56:53 am »
I have an issue with socket::connect() and socket::getRemoteAddres() in non-blocking mode (SFML 2.1 32bits Windows 7).

I'm trying to use a non-blocking socket on my client, because I don't want it to block on connect() when there is no host listening on the chosen port, as it's called every frame.

Basically my update loop is something like this:

while (isRunning)
{
    Update();
    SendData();
}

void SendData()
{
    // try to connect
    // if connected then do some socket.send()
}
 

The problem is that getRemoteAddress doesn't return None, even though it is not connected.
The documentation states: It the socket is not connected, this function returns sf::IpAddress::None.
http://www.sfml-dev.org/documentation/2.1/classsf_1_1TcpSocket.php#a7904ca6ab9e018021e305a3aeb7a1b9a

Here is how to reproduce the problem/bug:
sf::TcpSocket socket;
sf::Socket::Status socketStatus = sf::Socket::Error;
while (true)
{
        if (socketStatus != sf::Socket::Done)
        {
                socket.disconnect();
                socket.setBlocking(false);
                socketStatus = socket.connect(viewerIPAddress, viewerPort);
                sf::IpAddress remoteAddr = socket.getRemoteAddress();
                printf("ip: %s\n", remoteAddr.toString().c_str()); // if it couldn't connect it should be None
        }
}
 

Not that this is not exactly the code I'm running, I don't have a while loop trying to connect again and again, it's done only once per SendData() call, but this simpler example showcases the problem.

I was relying on what documentation says to know if my socket was connected (so I could send the data or try again to connect, without blocking the execution of the program).

Thanks for the help.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Issue with non-blocking socket and getRemoteAddress()
« Reply #1 on: August 19, 2013, 07:55:21 am »
Thanks, I'll test it.

Quote
The problem is that getRemoteAddress doesn't return None, even though it is not connected.
So what does it return?

Quote
I was relying on what documentation says to know if my socket was connected
Why don't you rely on the return value of the connect function?
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Issue with non-blocking socket and getRemoteAddress()
« Reply #2 on: August 19, 2013, 09:22:39 pm »
You're right. While the socket is connecting (connection not failed yet), it returns the address and port it's trying to connect to, instead of None and 0.

I should update the documentation.
Laurent Gomila - SFML developer

Romz

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Issue with non-blocking socket and getRemoteAddress()
« Reply #3 on: August 20, 2013, 01:23:04 am »
Thanks for the info.

I am now able to use non-blocking connect and blocking send.
I put my project (a real-time profiler/viewer) on github: https://github.com/shazbits/wiretap

Let me know if you want to list it somewhere. I added your libs (and license) to my repo, I hope that's ok with you.

Finally if you think something's not right with the way I connect/send, I'd appreciate any comments.
https://github.com/shazbits/wiretap/blob/326ef668afb0f434495f1945ee4459fb21a1ab0d/src/src/Profiler/WiretapProfiler.cpp#L57

 

anything