使用ServerSocket绑定已使用的端口时,Java 7不会引发BindException [英] Java 7 doesn't throw BindException when binding an already used port using ServerSocket
问题描述
我正在Windows 7 x64上的Java中使用ServerSocket进行实验. 我编写了一个小程序,该程序在端口8080上托管HTTP服务器,并且仅返回包含类加载器的toString()的静态HTML响应.
I'm experimenting on ServerSocket in Java on Windows 7 x64. I wrote a little program that host a HTTP server on port 8080 and only returns a static HTML response that contains the toString() of the class loader.
我在程序中所做的主要工作:
What I did in the program mainly:
- 创建一个ServerSocket
- 在serverSocket上调用setReuseAddress(false)
- 将端口8080绑定到此套接字
- 使用永久循环来接受套接字并给出响应
首先,我尝试使用JRE 1.6.0_23,一切都很棒:第一个实例启动并正常响应,由于抛出异常,第二个实例无法启动:
First I tried with JRE 1.6.0_23 and everything is great: first instance launched and responds normally, second instance cannot be launched since exception is thrown:
Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind
当我尝试使用JRE 1.7.0_5时发生了意外的事情:两个实例都可以成功启动,但是只有第一个实例可以响应.在第一个实例被杀死之后,第二个实例开始响应.
Unexpected thing happens when I tried with JRE 1.7.0_5: both instance can be launched successfully but only the first instance gives responses. After the first instance is kill, the second instance then starts to responds.
我做错什么了吗,或者这是JRE 7的错误吗?
Am I doing anything wrong or is this a bug of JRE 7?
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServerSocket {
private static final String HEADER = "HTTP/1.1 200 OK\r\n" + "Content-type: text/html\r\n"
+ "Connection: close\r\n" + "\r\n";
private static final int PORT = 8080;
private static void handle(Socket socket) {
System.out.println(socket.getInetAddress() + ":" + socket.getPort());
StringBuilder buffer = new StringBuilder();
buffer.append(HEADER);
buffer.append(TestServerSocket.class.getClassLoader());
try {
socket.getOutputStream().write(buffer.toString().getBytes());
} catch (IOException e) {
} finally {
try {
socket.close();
} catch (IOException e) {
}
}
}
public static void main(String[] args) throws IOException {
int port;
try {
port = Integer.parseInt(args[0]);
} catch (Exception e) {
port = PORT;
}
final ServerSocket server = new ServerSocket();
server.setReuseAddress(false);
server.bind(new InetSocketAddress(port));
// Terminator thread, stop when Ctrl-D is entered
new Thread() {
public void run() {
try {
while (System.in.read() != 4);
} catch (IOException e) {
e.printStackTrace();
}
try {
server.close();
} catch (IOException e) {
}
System.exit(0);
}
}.start();
System.out.println("Listening on: " + port);
Socket client = null;
while (true) {
try {
client = server.accept();
handle(client);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
推荐答案
要解决此问题,建议您运行以下测试代码. Apache HttpCore基本服务器.它是标准的API,并且在此特定示例中使用ServerSocket,因此它在您的环境中失败的可能性很小(Java 7).
To Isolate the problem, I would recommend that you run the following test code. Apache HttpCore basic server. It's standard API and uses ServerSocket in this particular example, so there is a very small chance that it would fail on your environment ( java 7).
万一它失败了,您肯定会知道问题出在您的代码上.同时,我将在我的工作机上的JDK 7上尝试您的代码并进行更新.
In case it fails you will know for sure problem is not with your code. Meanwhile I will try your code on JDK 7 on my work-machine and will update.
这篇关于使用ServerSocket绑定已使用的端口时,Java 7不会引发BindException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!