java - 如何在关闭后再次打开服务器套接字?

java - How to open server socket again after closing it ?

我在我的项目中使用 ServerSocket 对象。基本上,我有一个按钮可以关闭套接字并再次打开它。我怎样才能做到这一点 ?我可以关闭套接字。但是,无法打开它。

你不能那样做,看看 doc

特别是这一行:

Once a socket has been closed, it is not available for further networking use (i.e. can't be reconnected or rebound). A new socket needs to be created.

-


close

public void close() throws IOException Closes this socket. Any thread currently blocked in an I/O operation upon this socket will throw a SocketException.

Once a socket has been closed, it is not available for further networking use (i.e. can't be reconnected or rebound). A new socket needs to be created.

Closing this socket will also close the socket's InputStream and OutputStream.

If this socket has an associated channel then the channel is closed as well.

Throws: IOException - if an I/O error occurs when closing this socket. See Also: isClosed()