2

我在 linux 中使用服务器套接字,我需要在 time_wait TCP 状态到期之前关闭它并重新打开。我在绑定之前设置了服务器套接字的重用地址选项,但它仍然抛出 BindException。我也试过这个http://meteatamel.wordpress.com/2010/12/01/socket-reuseaddress-property-and-linux/但它仍然不起作用。

要打开服务器套接字,我使用:

ServerSocket ss = new ServerSocket();
ss.setReuseAddress(true);
ss.bind(new InetSocketAddress(12345));

并关闭:

ss.close();

“地址已在使用中”BindException 在绑定调用中被抛出。

此代码生成异常:

for (int i = 0; i < 2; i++) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    final ServerSocket ss = new ServerSocket();
                    ss.setReuseAddress(true);
                    ss.bind(new InetSocketAddress(12345));
                    Socket s = ss.accept();
                    System.out.println((char) s.getInputStream().read());
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        Thread.sleep(500);
        Socket s = new Socket("localhost", 12345);
        s.getOutputStream().write('c');
    }
4

2 回答 2

11

您在绑定之前设置重用,而不是在遇到异常之后。

ServerSocket ss = new ServerSocket(); // don't bind just yet
ss.setReuseAddress(true);
ss.bind(new InetSocketAddress(12345)); // can bind with reuse= true

这在 Windows 7 和 RHEL 5.x 上运行没有错误

for (int i = 0; i < 1000; i++) {
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                final ServerSocket ss = new ServerSocket();
                ss.setReuseAddress(true);
                ss.bind(new InetSocketAddress(12345));
                Socket s = ss.accept();
                System.out.println((char) s.getInputStream().read());
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    t.start();
    Thread.sleep(50);
    Socket s = new Socket("localhost", 12345);
    s.getOutputStream().write('c');
    t.join();
}
于 2012-05-09T12:33:57.800 回答
0

你必须这样做:这意味着提取代码,使其不会不断重复

public class....

private ServerSocket socServer;

onCreate
...

try {
    socServer = new ServerSocket();
    socServer.setReuseAddress(true);
    socServer.bind(new InetSocketAddress(SERVER_PORT));
} catch (IOException e) {
 e.printStackTrace();
}

// New thread to listen to incoming connections
new Thread(new Runnable() {
    @Override
    public void run() {
        try 
        {
            // Create server side client socket reference
            Socket socClient = null;

            // Infinite loop will listen for client requests to connect
            while (true) {
                // Accept the client connection and hand over communication 
                // to server side client socket
                   socClient = socServer.accept();

               // For each client new instance of AsyncTask will be created
                  ServerAsyncTask serverAsyncTask = new ServerAsyncTask();

               // Start the AsyncTask execution 
               // Accepted client socket object will pass as the parameter
               serverAsyncTask.execute(new Socket[] {socClient});
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}).start();
于 2017-04-19T10:24:55.793 回答