Java socket API: How to tell if a connection has been closed?

后端 未结 8 1424
迷失自我
迷失自我 2020-11-22 02:34

I am running into some issues with the Java socket API. I am trying to display the number of players currently connected to my game. It is easy to determine when a player h

8条回答
  •  隐瞒了意图╮
    2020-11-22 02:57

    It is general practice in various messaging protocols to keep heartbeating each other (keep sending ping packets) the packet does not need to be very large. The probing mechanism will allow you to detect the disconnected client even before TCP figures it out in general (TCP timeout is far higher) Send a probe and wait for say 5 seconds for a reply, if you do not see reply for say 2-3 subsequent probes, your player is disconnected.

    Also, related question

提交回复
热议问题