在我的应用程序中,我创建了一个ServerSocket
带有一些端口的实例。完成后,我关闭了套接字,但是当我尝试ServerSocket
在同一个端口上新建一个时,它会抛出:
"java.net.BindException: Address already in use"
如果我ServerSocket
使用不同的端口创建它,那么它可以工作。
ServerSocket.isClosed
也返回真
问题是什么?
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());
}
}