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

Author Topic: Non-blocking UDP Socket : Receive() after Send() always returns Disconnected  (Read 4913 times)

0 Members and 1 Guest are viewing this topic.

Dwarfobserver

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
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 ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
I'm not sure exactly what's wrong in your code, but it doesn't make sense. What are you trying to achieve?
Laurent Gomila - SFML developer

Dwarfobserver

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
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).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
You should not use the same socket for both ends of the communication. Do something that looks more like your actual code (two sockets).
Laurent Gomila - SFML developer

Dwarfobserver

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
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).
« Last Edit: December 17, 2017, 04:57:50 pm by Dwarfobserver »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
So where's the code of the other process involved in your test?
Laurent Gomila - SFML developer

Dwarfobserver

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
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)
« Last Edit: December 18, 2017, 02:51:25 am by Dwarfobserver »

Dwarfobserver

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
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

 

anything