【问题标题】:How to reconnect to a Server automatilly when server availble in java sockets当服务器在 java 套接字中可用时如何自动重新连接到服务器
【发布时间】:2015-08-26 11:22:21
【问题描述】:

我有 java 客户端服务器应用程序。服务器正在运行或可能未连接。但我们需要连接ip和端口。

Socket s=new Socket(ip,port);

如果已连接,则将创建套接字,否则将抛出 IOException。然后我将该连接的状态设置为未在对象中连接。 如果连接,我将发送数据作为命令。它将用数据响应该命令。我们使用输出流发送命令并使用套接字的输入流读取响应。

当服务器可用(有线或wifi连接)时,我需要自动重新连接到服务器。

这里如何使用多线程

例如:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
public class ConnectionThread implements Runnable {
    static class ConnectionStatus {
        boolean connected = false;

        public boolean isConnected() {
            return connected;
        }
        public void setConnected(boolean connected) {
            this.connected = connected;
        }
    }
    ConnectionStatus status;
    Socket socket;
    String ip;
    int port;
    public ConnectionThread(ConnectionStatus status, String ip, int port) {
        this.status = status;
        this.ip = ip;
        this.port = port;
    }
    public void connectAgain() throws IOException {
        while (!status.isConnected()) {
            socket = new Socket(ip, port);
            status.setConnected(true);
        }
    }
    public void run() {
        try {
            connectAgain();
        } catch (IOException e) {
            try {
                Thread.sleep(1000);
                connectAgain();
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        ConnectionStatus status = new ConnectionStatus();
        status.setConnected(false);
        ConnectionThread cthread = new ConnectionThread(status, "192.166.102.201", 1986);
        Thread x = new Thread(cthread);
        x.start();
        System.out.println("Connected ..." + status.isConnected());
    }
}

只有在连接时才需要发送命令(数据)以接收响应。

我需要在未连接时将连接状态设置为 false 并在连接时为真。

根据连接状态,我将从套接字读取或写入套接字流。

我需要知道连接何时失败 以及连接可用时。

我认为更改连接的属性时最好使用 propertychangelistener。

对吗?

【问题讨论】:

    标签: java multithreading sockets


    【解决方案1】:

    这个循环真的没有意义。

    while (!status.isConnected()) {
            socket = new Socket(ip, port);
            status.setConnected(true);
    }
    

    无论成功还是失败,它都不能多次执行循环。它的效果和这个完全一样:

    socket = new Socket(ip, port);
    status.setConnected(true);
    

    除非它没有抛出异常并成功,否则运行会以 1 秒的延迟调用它两次。如果您希望它继续尝试直到您连接,您需要在循环中捕获异常。例如

    Socket s = new Socket();
    SocketAddress sa = new InetSocketAddress("192.166.102.201", 1986);
    while (!s.isConnected()) {
        try {
            s.connect(sa); // try the connection
        } catch (IOException ex) {
            // ignore we may have to try lots of times
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
               // not sure how to handle this, maybe we should just give up. 
            }
        }
    }
    

    更新:我在同一个 try 块中同时拥有 sleep 和 connect,这意味着它永远不会休眠,只是保持线程始终忙碌。将睡眠移动到单独的块。

    【讨论】:

      猜你喜欢
      • 2021-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      • 2019-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多