java - 如果连接被拒绝,如何切换到另一台服务器?

标签 java connection telnet switching

如果当前连接、连接被拒绝或者当前 IP 不存在,我需要切换到另一个。这是部分代码:

String [] server= {"10.201.30.200", "10.66.20.70",}; // Servers array

public void Telnet(String[] server) {
    try {
        out2 = new PrintStream(new FileOutputStream("output.txt"));
        String [] hrnc = {"HRNC01_", "HRNC02_", "HRNC03_","NRNC01_", "NRNC02_"};

        for (int i = 0; i < server.length; i++) {        
            // Connect to the specified server
            if (server[i].equals("10.201.30.200")) { 
                // Commands via telnet
            } else if (server[i].equals("10.66.20.70")) {
                // Commands
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}    

例如,如果第一个 IP 不存在,我需要连接到下一个服务器。

最佳答案

尝试建立连接,捕获异常,如果发生则尝试另一个 IP 地址。

编辑: 当然!这是我的实现

package tests;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;

public class Connection {
    // Servers array
    private static final String[] server = { "10.201.30.200", "10.66.20.70" };
    // @logoff: default Telnet port
    private static final int port = 23;

    public static void telnet(String[] server) {
        PrintStream out2;
        try {

            out2 = new PrintStream(new FileOutputStream("output.txt"));

            String[] hrnc = { "HRNC01_", "HRNC02_", "HRNC03_", "NRNC01_",
                    "NRNC02_" };

            for (int i = 0; i < server.length; i++) {
                try {
                    // @logoff: try connection
                    Socket socket = new Socket(server[i], port);
                } catch (IOException e) {
                    // @logoff: typical "Connection timed out" or
                    // "Connection refused"
                    System.err.println("Error connecting to " + server[i]
                            + ". Error = " + e.getMessage());
                    // @logoff: continue to next for element
                    continue;
                }

                // Connect to the specified server
                if (server[i].equals("10.201.30.200")) {
                    // Commands via telnet
                }

                else if (server[i].equals("10.66.20.70")) {
                    // Commands
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // @logoff: your method returns void
        return;
    }

    public static void main(String[] args) {
        telnet(server);
    }
}

关于java - 如果连接被拒绝,如何切换到另一台服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13028249/

相关文章:

java - android隐藏内部RelativeLayout防止 Activity 出现

java - 线程 "main"javax.naming.NoInitialContextException : Cannot instantiate class 中出现异常

java - 如何在Java(TelNet)中实现接收套接字的事件?

Linux 外壳 : why is "open" required in this telnet command

telnet - 通过 telnet 使用 json-rpc?

java - GUI 可以并行检查大量数据的原始性

java - 禁用 JavaFX TextField 中的字符删除键

java - 扫描文本并替换(如果匹配)

connection - PuTTy 错误 : Unable to open connection, 网络错误无效参数

连接到 MySQL Linux 数据库的 C# 问题