BindException地址已在使用中

时间:2012-01-11 07:31:01

标签: android sockets

我遇到了serverThreadProc()方法的问题。每当我运行它时,它都会抛出一个BindException。认为try块的前两行可能存在问题。不知道我做错了什么。

private void serverThreadProc() {
    while (!this.stopRequested) {
        Socket sessionSocket = null;
        try {
            serverSocket = new ServerSocket(serverPort);
            sessionSocket = this.serverSocket.accept();
            InetAddress sourceAddress = sessionSocket.getInetAddress();
            Log.i(LOG_TAG, "Accepted new incoming connection ... "
                    + sourceAddress.toString());
            if (clientMap.containsKey(sourceAddress)) {
                ClientProcessor legacyProcessor = clientMap
                        .get(sourceAddress);
                legacyProcessor.shutdown();
            }
            ClientProcessor processor = new ClientProcessor(sessionSocket);
            processor.start();
            clientMap.put(sourceAddress, processor);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

public int start() {
    try {
        this.serverSocket = new ServerSocket(0);
        this.serverPort = this.serverSocket.getLocalPort();
        Log.i(LOG_TAG, "Server " + serverName + " started at "
                + this.serverSocket.getLocalSocketAddress());
    } catch (IOException ex) {
        Log.i(LOG_TAG, ex.toString());
    }

    this.serverThread.start();
    return this.serverPort;
}

public void stop() {
    try {
        stopRequested = true;
        if (this.serverSocket) {
            this.serverSocket.close();
        }
    } catch (IOException ex) {
        Log.i(LOG_TAG, ex.toString());
    }

    Log.i(LOG_TAG, "The server " + this.serverName + " has been stopped.");
}

以下是代码生成的堆栈跟踪:

01-11 05:51:53.472: I/ConnectActivity(2151): IP Address:0
01-11 05:51:53.492: I/ServiceServer(2151): Server Whooznear started at /0.0.0.0:35434
01-11 05:51:53.522: I/ConnectActivity(2151): Started at 35434
01-11 05:51:53.542: W/System.err(2151): java.net.BindException: Address already in use
01-11 05:51:53.542: W/System.err(2151):     at org.apache.harmony.luni.platform.OSNetworkSystem.bind(Native Method)
01-11 05:51:53.542: W/System.err(2151):     at dalvik.system.BlockGuard$WrappedNetworkSystem.bind(BlockGuard.java:275)
01-11 05:51:53.571: W/System.err(2151):     at org.apache.harmony.luni.net.PlainSocketImpl.bind(PlainSocketImpl.java:165)
01-11 05:51:53.571: W/System.err(2151):     at java.net.ServerSocket.<init>(ServerSocket.java:123)
01-11 05:51:53.592: W/System.err(2151):     at java.net.ServerSocket.<init>(ServerSocket.java:74)
01-11 05:51:53.592: W/System.err(2151):     at com.whooznear.android.ServiceServer.serverThreadProc(ServiceServer.java:63)
01-11 05:51:53.592: W/System.err(2151):     at com.whooznear.android.ServiceServer.access$0(ServiceServer.java:58)
01-11 05:51:53.611: W/System.err(2151):     at com.whooznear.android.ServiceServer$1.run(ServiceServer.java:53)

2 个答案:

答案 0 :(得分:1)

您在过程ServerSocket中创建start(),然后在过程ServerSocket中的同一端口上创建新的serverThreadProc()。在创建新的服务器套接字之前,必须关闭旧的。或者使用另一个端口。

答案 1 :(得分:0)

为什么你在方法while

中使用serverThreadProc()

简化程序。我认为你的代码有点混乱。

您可以参考基本套接字示例

这可以帮助您简化任务的代码