简体   繁体   English

Java套接字的问题

[英]Questions with java socket

In java, I can write code like this: 在Java中,我可以编写如下代码:

 ServerSocket ss = new ServerSocket(1111);
 Socket s = ss.accept();
 // here s.getLocalPort() is 1111
 ss.close();
 // here this is ok even s is still connected with a client.
 ss = new ServerSocket(s.getLocalPort());

on client side: 在客户端:

Socket s = new Socket("localhost", 1111);
// this line will throw an exception. 
ServerSocket ss = new ServerSocket(s.getLocalPort());

What I don't understand is the last line of the above two pieces of code seems no difference, why does it work differently? 我不明白的是,以上两段代码的最后一行似乎没有区别,为什么它的工作方式有所不同? any info is highly appreciated, thank you in advance. 任何信息都将受到高度赞赏,在此先感谢您。

在第一种情况下,您关闭了插座,在第二种情况下,您没有关闭插座,因此端口仍然被占用。

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

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