gpt4 book ai didi

Android ServerSocket EADDRINUSE

转载 作者:太空狗 更新时间:2023-10-29 12:48:54 28 4
gpt4 key购买 nike

我在 Android 上遇到 TCP 服务器问题。服务器必须管理多个传入连接,目前来自同一用户。我收到以下错误:

02-06 17:37:44.800: W/System.err(9859): java.net.BindException: bind failed: EADDRINUSE (Address already in use)
02-06 17:37:44.800: W/System.err(9859): at libcore.io.IoBridge.bind(IoBridge.java:89)
02-06 17:37:44.800: W/System.err(9859): at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:150)
02-06 17:37:44.800: W/System.err(9859): at java.net.ServerSocket.bind(ServerSocket.java:318)
02-06 17:37:44.800: W/System.err(9859): at java.net.ServerSocket.bind(ServerSocket.java:281)
02-06 17:37:44.800: W/System.err(9859): at sample.services.TCPService$1.run(TCPService.java:84)
02-06 17:37:44.804: W/System.err(9859): Caused by: libcore.io.ErrnoException: bind failed: EADDRINUSE (Address already in use)
02-06 17:37:44.804: W/System.err(9859): at libcore.io.Posix.bind(Native Method)
02-06 17:37:44.804: W/System.err(9859): at libcore.io.ForwardingOs.bind(ForwardingOs.java:39)
02-06 17:37:44.804: W/System.err(9859): at libcore.io.IoBridge.bind(IoBridge.java:87)
02-06 17:37:44.804: W/System.err(9859): ... 4 more

我添加了 setReuseAddress(true) 但没有任何改变。我正在使用的代码如下。错在哪里?谢谢

public void onStart(Intent intent, int startid) {
t = new Thread(){
public void run() {
try {
Log.d("TCP", "Server: Creating server.");
ServerSocket ss = new ServerSocket();
ss.setReuseAddress(true);
ss.bind(new InetSocketAddress(TCPPORT));
while(true) {
//Server is waiting for client here, if needed
Log.d("TCP", "Server: Waiting on packet!");
Socket s = ss.accept();
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
String msg = input.readLine();
......
}
}
};
t.start();
}

最佳答案

我解决了!解决方案是必须在 setReuseAddress() 语句之后绑定(bind)套接字。

关于Android ServerSocket EADDRINUSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14735032/

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