简体   繁体   English

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

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

I'm using ServerSocket object in my project. 我在我的项目中使用ServerSocket对象。 Basically, I have a button which closes socket and open it again. 基本上,我有一个关闭插座并再次打开它的按钮。 How can I do this ? 我怎样才能做到这一点 ? I can close socket. 我可以关闭套接字。 However, cannot open it. 但是,无法打开它。

You can not do that, take a look to the doc 你不能这样做,看看文档

particullarly this line: 特别是这一行:

Once a socket has been closed, it is not available for further networking use (ie can't be reconnected or rebound). 一旦套接字关闭,它就无法用于进一步的网络连接(即无法重新连接或反弹)。 A new socket needs to be created. 需要创建一个新的套接字。

- -


close

public void close() throws IOException Closes this socket. public void close()throws IOException关闭此套接字。 Any thread currently blocked in an I/O operation upon this socket will throw a SocketException. 当前在此套接字上的I / O操作中阻塞的任何线程都将抛出SocketException。

Once a socket has been closed, it is not available for further networking use (ie 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. 关闭此套接字也将关闭套接字的InputStream和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. 抛出:IOException - 如果关闭此套接字时发生I / O错误。 See Also: isClosed() 另见:isClosed()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM