如何让客户端等待服务器套接字连接?

How to make the client wait for the server socket connection?

我正在使用 wifi direct 在两个移动设备之间进行通信。我有设置套接字来发送和接收消息。如果我先启动服务器设备(所有者)然后启动客户端设备(成员),一切正常。我想以任何顺序启动任何设备并仍然设法发送消息。

目前,客户端等待 5 秒,如果没有可用连接,则停止寻找服务器。我希望客户端在超时后继续寻找服务器,直到找到服务器连接。我试过这样做,但我还没有成功实施。

初始客户端代码:

@Override
    public void run() {
        Socket socket = new Socket();
        try {
            socket.bind(null);
            socket.connect(new InetSocketAddress(address.getHostAddress(), SERVER_PORT), TIME_OUT);
            socketCreator = new SocketCreator(socket, handler);
            new Thread(socketCreator).start();
        } catch (IOException e) {
            e.printStackTrace();
            try {
                socket.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return;
        }
    }

修改为:

 @Override
    public void run() {
        Socket socket = new Socket();
        try {
            socket.setReuseAddress(true);
            socket.bind(null);
            while(!connectedToServer) {
                try {
                    socket.connect(new InetSocketAddress(address.getHostAddress(), SERVER_PORT),
                            TIME_OUT);
                    socketCreator = new SocketReadWriter(socket, handler);
                    new Thread(socketCreator).start();
                    connectedToServer = true;
                } catch (ConnectException e) {
                    Log.i(TAG,"Error while connecting. " + e.getMessage());
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                } catch (Exception e){
                    Log.i(TAG,"Exception "+e.getMessage());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            try {
                socket.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

我的代码中缺少什么?我怎样才能让它发挥作用?

我不断收到此错误:

Exception java.io.IOException: fcntl failed: EBADF (Bad file number)

您每次都必须创建一个 new Socket()。无法连接到远程服务器会使套接字失效,无法进一步使用。

If connect() fails, consider the state of the socket as unspecified. Portable applications should close the socket and create a new one for reconnecting. http://man7.org/linux/man-pages/man2/connect.2.html

因为你在 Android 上 运行 - 我猜你在尝试调用 connect 两次时没有看到 "Socket closed" 错误。

@Override
public void run()
{
    connector: while (true)
    {
        Socket socket = new Socket();

        try
        {
            socket.connect(new InetSocketAddress("0.0.0.0", 9999), 2000);
            this.doSomethingElseWithConnectedSocket(socket);
            break connector; // break out of the while loop
        }
        catch (ConnectException e)
        {
            // ignore because worthless
        }

        try
        {
             socket.close();
        }
        catch(Throwable x)
        {
             // ignore
        }

        System.out.println("Unable to connect to server ");

        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e1)
        {
            // ignore
        }
    }
}

public void doSomethingElseWithConnectedSocket(Socket value)
{
     value.close(); // close for no good reason
}