问题描述
我遇到了一些Java套接字API的问题。我试图显示当前连接到我的游戏的玩家数量。很容易确定玩家何时连接。但是,确定播放器何时使用套接字API断开连接似乎不必要的困难。
调用 isConnected()
在远程断开连接的套接字总是似乎返回 true
。类似地,在远程关闭的套接字上调用 isClosed()
总是似乎返回 false
。我已经读过,实际确定一个套接字是否已经关闭,数据必须写入输出流,并且必须捕获异常。这似乎是一个真正不干净的方式来处理这种情况。
有没有其他解决方案?
没有TCP API会告诉你连接的当前状态。 isConnected()
和 isClosed()
告诉您套接字的当前状态 。不一样。
-
isConnected()
em 已连接此套接字。 -
isClosed()
会告诉您 / em>已关闭此套接字。
-
read c $ c>返回-1
-
readLine()
返回null
/ li>
-
readXXX()
throwsEOFException
/ p> -
写入操作会抛出
IOException
:'peer reset by peer'
-
-
如果连接由于任何其他原因丢弃,写入会抛出<$ c $
-
如果对等体仍然连接,但是如果对等体仍然连接,
-
与您在其他地方可能阅读的内容相反,
ClosedChannelException
不告诉你这一点。 [SocketException:socket closed。
]它只是告诉你关闭了通道,然后继续用它。换句话说,你的一个编程错误。 -
由于在Windows XP上使用Java 7进行了一些实验, :
- 您选择
OP_READ
-
select()
返回大于零的值 - 相关的
SelectionKey
已无效(key.isValid()== false
)
这意味着对等体已重置连接。但是这可能是JRE版本或平台特有的。
- 您选择
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 has connected. However, it seems unnecessarily difficult to determine when a player has disconnected using the socket API.
Calling
isConnected()
on a socket that has been disconnected remotely always seems to return true
. Similarly, calling isClosed()
on a socket that has been closed remotely always seems to return false
. I have read that to actually determine whether or not a socket has been closed, data must be written to the output stream and an exception must be caught. This seems like a really unclean way to handle this situation. We would just constantly have to spam a garbage message over the network to ever know when a socket had closed.
Is there any other solution?
解决方案
There is no TCP API that will tell you the current state of the connection.
isConnected()
and isClosed()
tell you the current state of your socket. Not the same thing.
isConnected()
tells you whether you have connected this socket. You have, so it returns true.isClosed()
tells you whether you have closed this socket. Until you have, it returns false.If the peer has closed the connection in an orderly way
read()
returns -1readLine()
returnsnull
readXXX()
throwsEOFException
for any other XXX.A write will throw an
IOException
: 'connection reset by peer', eventually, subject to buffering delays.
If the connection has dropped for any other reason, a write will throw an
IOException
, eventually, as above, and a read may do the same thing.If the peer is still connected but not using the connection, a read timeout can be used.
Contrary to what you may read elsewhere,
ClosedChannelException
doesn't tell you this. [Neither doesSocketException: socket closed.
] It only tells you that you closed the channel, and then continued to use it. In other words, a programming error on your part. It does not indicate a closed connection.As a result of some experiments with Java 7 on Windows XP it also appears that if:
you're selecting on
OP_READ
select()
returns a value of greater than zerothe associated
SelectionKey
is already invalid (key.isValid() == false
)
it means the peer has reset the connection. However this may be peculiar to either the JRE version or platform.
这篇关于Java套接字API:如何判断连接是否已经关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!