读取期间的Java套接字超时 [英] Java socket timeout during read
问题描述
如果通过 Socket
连接的客户端出现延迟,会发生什么?
假设我从TCP服务器调用 socket.read()
,并且客户端在网络管道上写入一些内容,但是他的连接正在为1或者2分钟。会发生什么 ? 读取
会失败吗?或者它会等待吗?
What happens if a client connected through a Socket
has a bad lag ?
Let's say i call socket.read()
from the TCP server, and the client writes some stuff on the network pipe, but his connection is laging for 1 or 2 minutes. What will happen ? Will the read
fail ? Or will it wait ?
我不确定它是否可能,但我在FICS服务器上玩在线国际象棋,有时它似乎发生在我的观点view(我只是这个国际象棋服务器的用户)。
I'm not sure if it's even possible, but i'm playing online chess on FICS server and sometimes it seems to happen from my point of view (I'm only a user of this chess server).
我问这个是因为我在网络游戏上工作,我想处理这种情况以这种或那种方式。但首先我需要知道:
I'm asking this because i'm working on an online game and i'd like to handle such cases one way or another. But first I need to know if:
- 它可能发生
- 可以检测到 li>
- it can happen
- it can be detected
谢谢
推荐答案
我是建议不要仅仅依赖于设置 SO_TIMEOUT
变量,而是在客户端和服务器之间添加应用程序级心跳。如果客户端或服务器在指定时间内没有收到心跳消息(例如,在心跳频率的两倍之内),它应该切断连接,并在客户端的情况下尝试重新连接。
I'd advise not relying solely on setting the SO_TIMEOUT
variable but rather add application-level heartbeats between your client and server. If either the client or server does not receive a heartbeat message within a specified time (e.g. within twice the heartbeat frequency) it should sever the connection, and in the client's case attempt a reconnect.
使用心跳机制还可以通过监视每端接收心跳消息的延迟来测量延迟。
Using a heartbeat mechanism also allows you to measure the lag by monitoring the delay in receiving heartbeat messages on each end.
这篇关于读取期间的Java套接字超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!