在我的應用程序創建一個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());
}
}
我等待10分鐘,但它仍然不是讓MT使用此端口agien直到我結束我的服務器程序... – dan 2011-05-27 07:56:48