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

Author Topic: Receiving packets: while(socket.receive()...) vs. if(socket.receive()...)  (Read 4440 times)

0 Members and 1 Guest are viewing this topic.

eggw

  • Newbie
  • *
  • Posts: 9
    • View Profile
I was experiencing some issues in my current project with latency delays while testing locally. Then I came upon this thread, specifically this message:

https://en.sfml-dev.org/forums/index.php?topic=12011.msg91201#msg91201

Changing my if's to while's fixed it. I was just wondering why it made such a huge difference. Is this a bug, or is there an intentional reason/explanation for why this occurs?

Thanks!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Receiving packets: while(socket.receive()...) vs. if(socket.receive()...)
« Reply #1 on: December 01, 2018, 12:14:40 pm »
Do you understand what the change does?
How does it change your application's flow?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

eggw

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Receiving packets: while(socket.receive()...) vs. if(socket.receive()...)
« Reply #2 on: December 01, 2018, 01:56:45 pm »
That's why I created this topic  ;)

I'm currently using both a mixture of TCP and UDP sockets for a personal project (TCP only for the few, occasional packets I send that need to be guaranteed to arrive, UDP for position updates) . I'm using both in non-blocking mode, so I'm not sure exactly what effect the change really had. It didn't change any other behaviour; all that happened was that the delays vanished. Sorry if I'm missing something simple, this is my first foray in network programming.
« Last Edit: December 01, 2018, 01:59:05 pm by eggw »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Receiving packets: while(socket.receive()...) vs. if(socket.receive()...)
« Reply #3 on: December 01, 2018, 05:52:51 pm »
With 'if' you only process one packet. If there are several waiting (in the system buffer) then they'll have to wait until the next few iterations of your game loop -- yes, this is latency ;)
Laurent Gomila - SFML developer

eggw

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Receiving packets: while(socket.receive()...) vs. if(socket.receive()...)
« Reply #4 on: December 01, 2018, 07:07:35 pm »
With 'if' you only process one packet. If there are several waiting (in the system buffer) then they'll have to wait until the next few iterations of your game loop -- yes, this is latency ;)

Thank you very much, that clears things up!  ;D

As a follow-up question, I just noticed something odd (not sure if this goes beyond the scope of SFML). I'm currently testing my networked program by running two instances of it, one which hosts and connects to itself via LocalHost in the same process, and the second which connects to the first via LocalHost as well.

Things appear to work when I test things with two instances of the GCC-compiled executables. However, when I try it with two MSVC-compiled executables, there is a significant delay in the packet sending. Interestingly enough, it also works if I try a MSVC host and a GCC client, but the other way around still lags. It's the exact same code, so I'm assuming MSVC is doing something non-standard. Are there any known quirks that would explain the discrepancy in behaviour between these two executables?


Edit: nevermind, I have found the issue. Thank you again for your time!
« Last Edit: December 01, 2018, 08:11:01 pm by eggw »