gpt4 book ai didi

android - BindException 地址已在使用中

转载 作者:行者123 更新时间:2023-11-29 02:08:13 24 4
gpt4 key购买 nike

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

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)

最佳答案

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

关于android - BindException 地址已在使用中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8815665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com