调用 ServerSocket.close() 是否足以关闭端口?
我有一些与此类似的 java 代码:
private void startServer() throws IOException {
URLClassLoader classloader = null;
System.out.println("Opening server socket for listening on " + PORT_NUMBER);
try {
server = new ServerSocket(PORT_NUMBER);
server.setSoTimeout(10000);
connected = true;
System.out.println("Server is now listening on port " + PORT_NUMBER);
} catch (IOException e) {
System.err.println("Could not start server on port " + PORT_NUMBER);
e.printStackTrace();
connected = false;
}
while (connected) {
// Incoming request handler socket.
Socket socket = null;
try {
System.out.println("Waiting for client connection...");
// Block waiting for an incoming connection.
socket = server.accept();
if (socket == null) continue;
...等等。当我稍后调用 server.close() 时(如果我先调用 socket.close(),我不会得到任何不同的行为),我没有收到任何错误,但 netstat 显示该端口仍在侦听。调用 ServerSocket.close() 是否足以释放该系统上的端口?
我正在为 Java 1.4.2 微版本运行时编程。还值得注意的是,我有这个方法在另一个线程中运行,并且我试图从其父线程关闭套接字。
编辑 这是来自 netstat 的行,但我可以向您保证它仍在被侦听,因为如果我再次启动 Xlet,我会收到该端口号的异常。
tcp 0 0 *.2349 *.* LISTEN
I have some java code that looks similar to this:
private void startServer() throws IOException {
URLClassLoader classloader = null;
System.out.println("Opening server socket for listening on " + PORT_NUMBER);
try {
server = new ServerSocket(PORT_NUMBER);
server.setSoTimeout(10000);
connected = true;
System.out.println("Server is now listening on port " + PORT_NUMBER);
} catch (IOException e) {
System.err.println("Could not start server on port " + PORT_NUMBER);
e.printStackTrace();
connected = false;
}
while (connected) {
// Incoming request handler socket.
Socket socket = null;
try {
System.out.println("Waiting for client connection...");
// Block waiting for an incoming connection.
socket = server.accept();
if (socket == null) continue;
...and so on and so forth. When I call server.close() later on (I don't get any different behavior if I call socket.close() first), I don't get any errors, but netstat shows that the port is still being listened on. Should calling ServerSocket.close() be sufficient enough to free up the port on this system?
I am programming for a Java 1.4.2 microedition runtime. It is also worthy to note that I have this method being run in another thread, and I am trying to close the socket from its parent thread.
EDIT Here is the line from netstat, though I can assure you it is still being listened on, since if I start the Xlet again I get an exception with that port number.
tcp 0 0 *.2349 *.* LISTEN
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几件事需要考虑。其中之一是通过以下来自 ServerSocket 的 JavaDoc 的引用来描述的
因此,在您
close()
服务器套接字之后,操作系统仍然可以显示正在发生某些事情,这是可以接受的。但是,如果您要频繁打开/关闭同一端口上的服务器套接字,则可能会遇到问题。There are several things to consider. One of them is described by the following quotation from JavaDoc of ServerSocket
So it is kind of OK that the OS can still show that there is something going on after you
close()
the server socket. But if you going to open/close a server socket on the same port frequently you might hit a problem.