继续发出恼人的警告"绑定失败:EADDRINUSE(地址已在使用中)"

时间:2016-01-05 15:42:39

标签: java android

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

  

01-05 13:19:30.759:W / System.err(3371):java.net.BindException:bind   失败:EADDRINUSE(地址已在使用中)

     

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):引起:   libcore.io.ErrnoException:绑定失败:EADDRINUSE(地址已经在   使用)

     

01-05 13:19:30.759:W / System.err(3371):at   libcore.io.Posix.bind(本机方法)

     

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更多

我正在使用此代码:

移动代码:

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();

1 个答案:

答案 0 :(得分:1)

编辑(感谢提图斯)

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

    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();
        }

    }