In java, I can write code like this:
ServerSocket ss = new ServerSocket(开发者_StackOverflow社区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.
In the first case you closed the socket, in the second you didn't, so the port is still occupied.
精彩评论