使用后关闭所有的套接字?在服务器端

问题描述:

使用它之后,我必须关闭所有的套接字吗?我应该在哪里把它们放在这个代码中?当我运行它时,我的程序正常运行。但是,当我重新运行它时,它说“线程中的异常”主“java.net.BindException:已经在使用的地址:JVM_Bind”。因此,我认为在使用它之后我没有关闭所有的套接字。使用后关闭所有的套接字?在服务器端

import java.io.*; 
import java.net.*; 
import java.util.*; 
public class Server2 { 
public static void main(String args[]) throws Exception { 
    int PORT = 5555;  // Open port 5555 
    //open socket to listen 
    ServerSocket server = new ServerSocket(PORT); 
    Socket client = null; 
    while (true) { 
     System.out.println("Waiting for client..."); 
     // open client socket to accept connection 
     client = server.accept(); 
     System.out.println(client.getInetAddress()+" contacted "); 
     System.out.println("Creating thread to serve request"); 
     ServerStudentThread student = new ServerStudentThread(client); 
     student.start(); 


    } 

} 

}

+0

你似乎已经回答了你自己的问题。你有没有尝试过自己的建议? – 2011-02-12 06:24:37

+0

@Marcelo Cantos:但我不知道如何关闭套接字以及在哪里放置它。我把socket.close()放在哪里,它说错了。 – John 2011-02-12 06:42:39

+0

你需要一个有关闭钩子,每当你收到进程终止信号,然后设置exitServer为true,这应该是一个易变的变量,并发生在单独的线程。也看看Tomcat HttpServer的源代码,你可能会有一个想法! ! – 2011-02-12 09:21:23

finally区块中调用server.close()

ServerSocket server = new ServerSocket(PORT); 
try { 
    while (true) { 
     System.out.println("Waiting for client..."); 
     // open client socket to accept connection 
     Socket client = server.accept(); 
     System.out.println(client.getInetAddress()+" contacted "); 
     System.out.println("Creating thread to serve request"); 
     ServerStudentThread student = new ServerStudentThread(client); 
     student.start(); 
    } 
} finally { 
    server.close(); 
} 

Address already in use: JVM_Bind - 手段,你的操作系统是不是以前使用后关闭插座。它在超时约30-180秒时关闭。

我不知道真的要如何在Java中做到这一点,但在C代码可以做到这样的,以前bind系统功能调用:

int yes = 1; 
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); 

意思:设置标志(选项) SO_REUSEADDRsockfd插座。

在java中必须存在适当的机制。

  1. 您正在运行一个无限while循环,有一个布尔变量来什么时候停止,我认为你是不正常退出,这就是为什么端口未关闭。

可能是你可以尝试这样的

import java.io.*; 
import java.net.*; 
import java.util.*; 
public class Server2 { 
static int NUM_CONN_TO_WAIT_FOR=15; 
boolean exitServer =false; 
public static void main(String args[]) throws Exception { 
    int PORT = 5555;  // Open port 5555 
    //open socket to listen 
    ServerSocket server = new ServerSocket(PORT); 
    Socket client = null; 
    static int connections =0; 
try 
{ 

    while (!exitServer) { 
     System.out.println("Waiting for client..."); 
     // open client socket to accept connection 
     if (connections < NUM_CONN_TO_WAIT_FOR) 
     { 
      client = server.accept(); 
      System.out.println(client.getInetAddress()+" contacted "); 
      System.out.println("Creating thread to serve request"); 
      ServerStudentThread student = new ServerStudentThread(client); 
      student.start(); 
     } else 
     { 
      exitServer =true; 
     } 
     connections++; 

    } 
} catch (Exception e) 
{ 
    System.out.println(e.printStackTrace()); 
} 
finally 
{ 

    if (client != null) 
     client.close(); 

    if (server!= null) 
     server.close(); 

} 

} 

}