c++ - UDP Server stop receiving data -


i trying make online simple game, when test game on localhost , there no problem server , client when try connect pc laptop on local network start receiving data few seconds after stopped.

here code:

server
client

your problem udp unreliable, , sockets default blocking.

so think situation:

  1. server blocked in recvfrom waiting packet client
  2. the client sends packet, dropped , never reaches server
  3. the client goes on it's own recvfrom call blocks.

now have deadlock both server and client blocked in recvfrom.

for simple game yours might not need reliability, it's okay if packet here or there doesn't arrive. important don't block deadlock situation might occur.

there 2 solutions this: first make sockets non-blocking, , handle case recvfrom doesn't receive anything. take care here though, threads doesn't sleeping consume quite lot of cpu power.

the second solution use polling e.g. select see when can read socket.


Comments