使用服务器套接字后如何关闭端口
在我的应用程序中,我创建了一个带有某个端口的 ServerSocket 实例。完成后,我关闭套接字,但是当我尝试在同一端口上创建新的 ServerSocket
时,它会抛出:
"java.net.BindException: Address already in use"
如果我使用不同的端口创建 ServerSocket
,然后就可以了。
ServerSocket.isClosed
也返回 true
有什么问题?
public void run() {
try {
BufferedInputStream bufferedinputstream = new BufferedInputStream(
new FileInputStream(fileReq));
BufferedOutputStream outStream = new BufferedOutputStream(
cs.getOutputStream());
byte buffer[] = new byte[1024];
int read;
System.out.println(cs);
while ((read = bufferedinputstream.read(buffer)) != -1)
{
outStream.write(buffer, 0, read);
outStream.flush();
}
System.out.println("File transfered");
outStream.close();
bufferedinputstream.close();
try {
this.finalize();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
System.out.println("Exce....");
System.out.println(e.getMessage());
}
finally
{
if ( cs != null)
try {
int usedPort=cs.getLocalPort();
System.out.println("Closing "+cs);
cs.close();
System.out.println(cs+" Closed");
System.out.println("asd"+cs.isClosed());
portManager.getInstance().mp.put(usedPort,true);
} catch (IOException e) {
System.err.println("in sendToClient can't close sockt");
System.err.println(e.getMessage());
}
}
In my application I create a ServerSocket
instance with some port. After I am finished, I close the socket, but when I try to make a new ServerSocket
on the same port, it throws:
"java.net.BindException: Address already in use"
If I create the ServerSocket
with a different port, then it works.
ServerSocket.isClosed
also returns true
What is the problem?
public void run() {
try {
BufferedInputStream bufferedinputstream = new BufferedInputStream(
new FileInputStream(fileReq));
BufferedOutputStream outStream = new BufferedOutputStream(
cs.getOutputStream());
byte buffer[] = new byte[1024];
int read;
System.out.println(cs);
while ((read = bufferedinputstream.read(buffer)) != -1)
{
outStream.write(buffer, 0, read);
outStream.flush();
}
System.out.println("File transfered");
outStream.close();
bufferedinputstream.close();
try {
this.finalize();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
System.out.println("Exce....");
System.out.println(e.getMessage());
}
finally
{
if ( cs != null)
try {
int usedPort=cs.getLocalPort();
System.out.println("Closing "+cs);
cs.close();
System.out.println(cs+" Closed");
System.out.println("asd"+cs.isClosed());
portManager.getInstance().mp.put(usedPort,true);
} catch (IOException e) {
System.err.println("in sendToClient can't close sockt");
System.err.println(e.getMessage());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否尝试过在服务器套接字上调用
setReuseAddress(true)
? TCP状态机进入TIME_WAIT
状态是正常的。有关详细说明,请参阅此处。Have you tried calling
setReuseAddress(true)
on the server socket? It's normal for the TCP state machine to enterTIME_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...
完成后只需关闭套接字即可。它会自动关闭正在侦听的端口。完成后切勿保持打开套接字(端口)。
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.
这意味着服务器已经打开,您需要检查用户输入或客户端输入,表明关闭服务器,就像
一旦服务器打开,它就会打开,直到有东西将其关闭。
This means that the server is already open you need a check for user input or client input that says close the server like
Once you have the server open its just open until something closes it.