How to close port after using server sockets [closed]

↘锁芯ラ 提交于 2019-12-01 06:11:01

Have you tried calling setReuseAddress(true) on the server socket? It's normal for the TCP state machine to enter TIME_WAIT state. For detailed explanation look here.

It is because there is a cooldown period implemented on some O.S. If you wait a little (a couple of minutes for example), then you should be able to access the port again.

That being said, it is not a good idea to open and close serversockets on a port. It is better to open it and keep it open as long as necessary. Then close it when you are done. Don't open/close/open/close/open/close...

AjayR

Just close the socket when done. It automatically shuts down the port it is listening on. Never keep open the socket (port) when you are done.

Vasman

This means that the server is already open you need a check for user input or client input that says close the server like

while( bufferedinputstream != "quit" )

try{

...

}

Once you have the server open its just open until something closes it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!