gpt4 book ai didi

java - 不断收到烦人的警告 "bind failed: EADDRINUSE (Address already in use)"

转载 作者:行者123 更新时间:2023-12-02 04:03:30 38 4
gpt4 key购买 nike

我正在将移动设备与 Java 应用程序连接,但我不断收到此警告:

01-05 13:19:30.759: W/System.err(3371): java.net.BindException: bind failed: EADDRINUSE (Address already in use)

01-05 13:19:30.759: W/System.err(3371): at libcore.io.IoBridge.bind(IoBridge.java:89)

01-05 13:19:30.759: W/System.err(3371): at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:150)

01-05 13:19:30.759: W/System.err(3371): at java.net.ServerSocket.bind(ServerSocket.java:318)

01-05 13:19:30.759: W/System.err(3371): at java.net.ServerSocket.bind(ServerSocket.java:281)

01-05 13:19:30.759: W/System.err(3371): at com.example.informativoapp.ConnectThread.run(ConnectThread.java:32)

01-05 13:19:30.759: W/System.err(3371): Caused by: libcore.io.ErrnoException: bind failed: EADDRINUSE (Address already in use)

01-05 13:19:30.759: W/System.err(3371): at libcore.io.Posix.bind(Native Method)

01-05 13:19:30.759: W/System.err(3371): at libcore.io.ForwardingOs.bind(ForwardingOs.java:39)

01-05 13:19:30.759: W/System.err(3371): at libcore.io.IoBridge.bind(IoBridge.java:87)

01-05 13:19:30.759: W/System.err(3371): ... 4 more

我正在使用此代码:

移动代码:

private ConnectedThread con;
private ConnectThread connect;

...

protected void onCreate(Bundle savedInstanceState) {
...
connect = new ConnectThread(mHandler);

connect.start();
}

public void onBackPressed() {
if(connect != null)
connect.interrupt();
if(con != null)
con.interrupt();

finish();
}


public class ConnectThread extends Thread {

Handler mHandler;
private ConnectedThread con;

public ConnectThread(Handler mHandler)
{

this.mHandler = mHandler;

}

@Override
public void run()
{
while(true){
Socket socket=null;
try {
ServerSocket serverSocket = new ServerSocket();
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(4200));
socket = serverSocket.accept();

Log.i("conex", "Conectou");

mHandler.obtainMessage(3,socket).sendToTarget();


} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

}

处理程序执行它:

                    con = new ConnectedThread((Socket)msg.obj, mHandler);                       
con.start();

最佳答案

已编辑(感谢泰特斯)

可能,您的代码每次都会创建套接字。请尝试这个。

    Socket socket=null;
try {
ServerSocket serverSocket = new ServerSocket();
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(4200));
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

while(true){
try {
Log.i("conex", "Conectou");

socket = serverSocket.accept();
mHandler.obtainMessage(3,socket).sendToTarget();


} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

关于java - 不断收到烦人的警告 "bind failed: EADDRINUSE (Address already in use)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34615588/

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