SFML community forums

Help => Network => Topic started by: Dwarfobserver on December 17, 2017, 06:35:31 am

Title: Non-blocking UDP Socket : Receive() after Send() always returns Disconnected
Post by: Dwarfobserver on December 17, 2017, 06:35:31 am
Hello,

First, thanks for this library and sorry if my question has an obvious / documented answer but I didn't find anything. Here is a code sample that recreates my issue (which is, getting a Disconnected socket status when calling socket.Receive(...) after socket.Send(...), even if I take a nap between the calls) :

sf::UdpSocket socket;
socket.bind(sf::Socket::AnyPort);
socket.setBlocking(false);

std::byte buffer[2000];
sf::IpAddress remoteIp = sf::IpAddress::LocalHost;
uint16_t remotePort = 42913;
size_t receivedSize;

assert(socket.getLocalPort() != remotePort);

std::this_thread::sleep_for(100ms);

// Status = Done
socket.send(buffer, 500, remoteIp, remotePort);

std::this_thread::sleep_for(100ms);

// Status = Disconnected
socket.receive(buffer, 1000, receivedSize, remoteIp, remotePort);

 

Maybe it has to do with sending the packet to myself, but it is very conveniant. Any idea ?
Title: Re: Non-blocking UDP Socket : Receive() after Send() always returns Disconnected
Post by: Laurent on December 17, 2017, 10:37:49 am
I'm not sure exactly what's wrong in your code, but it doesn't make sense. What are you trying to achieve?
Title: Re: Non-blocking UDP Socket : Receive() after Send() always returns Disconnected
Post by: Dwarfobserver on December 17, 2017, 03:44:39 pm
The Receive(...) call is supposed to returns "Not Ready" status, or "Done" if there is packet to poll (which happens as excepted until I call Send(...)). It's for a game, where I will send and receive informations in a loop. The code I wrote is only a minimal exemple to show my problem (there is another process that will communicate in the same way, to test my client / server code).
Title: Re: Non-blocking UDP Socket : Receive() after Send() always returns Disconnected
Post by: Laurent on December 17, 2017, 04:13:21 pm
You should not use the same socket for both ends of the communication. Do something that looks more like your actual code (two sockets).
Title: Re: Non-blocking UDP Socket : Receive() after Send() always returns Disconnected
Post by: Dwarfobserver on December 17, 2017, 04:47:22 pm
I do not use the same socket for my two process (they are bound to different ports).

In fact, I ran exactly the given code and got the problem (of the Receive(...) call returning Disconect, the only difference was that I stdout'ed the status), and running the second process don't change the output of this one (or sending a packet with a second socket in the same process).
Title: Re: Non-blocking UDP Socket : Receive() after Send() always returns Disconnected
Post by: Laurent on December 17, 2017, 08:01:36 pm
So where's the code of the other process involved in your test?
Title: Re: Non-blocking UDP Socket : Receive() after Send() always returns Disconnected
Post by: Dwarfobserver on December 17, 2017, 11:27:51 pm
What I tried to say is that, running or not another process do not change the output of the minimal program I wrote in my first post.

To put it simply : I want to know what I did wrong in the code sample (which is all I need in a main to recreates the problem), and if it works for anyone, know from where the error can come (can a wrong library bind cause something like this ? I use MinGW by the way)
Title: Re: Non-blocking UDP Socket : Receive() after Send() always returns Disconnected
Post by: Dwarfobserver on December 18, 2017, 06:29:19 am
Well, it turned out that my compiler was probably the problem (when trying asio sockets), aka a recent mingw64 with C++17 support (found on SourceForge). Sorry for the troubles then !


... It was such a wasted week-end XD