1

I need to switch to another if on current connection, connection refused, or if current IP do not exist. Here is a part of code:

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

For example if first IP don't exist i need to connect to the next server.

Samir Maksimov
  • 105
  • 1
  • 2
  • 9
  • 1
    what have you tried so far? What I mean is, if you have tried something, how does it fail, what did you expect and what actually happened. – Jon Taylor Oct 23 '12 at 10:19
  • If i connect to the ip that do not exist i have an error - "java.net.ConnectException: Connection refused: connect" and my programm stop. I need to switch to another IP if current connection refused – Samir Maksimov Oct 23 '12 at 10:22
  • So you have to stop your programming from stopping on that exception and make it continue instead. This is really not the place to be learning basic Java. – user207421 Oct 23 '12 at 10:31

2 Answers2

1

Try to do the connection, capture exceptions and if it happen try another IP Address.

EDIT: Of course! This is my implementation

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);
    }
}
logoff
  • 3,347
  • 5
  • 41
  • 58
1

Try putting each connection attempt in a try/catch statement

SpaceCowboy
  • 543
  • 6
  • 16