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

问题描述 投票:0回答:1

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

目前,客户端等待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)
java sockets serversocket wifip2p
1个回答
1
投票

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

如果connect()失败,请将套接字的状态视为未指定。便携式应用程序应关闭套接字并创建一个新的套接字以重新连接。 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
}
© www.soinside.com 2019 - 2024. All rights reserved.