java在错误的ip地址打开一个套接字

时间:2015-12-29 14:51:50

标签: java sockets

有2台后端机器可以提供相同的内容。我尝试创建循环设置并进行故障保护,以便在发生故障时将请求发送给其他人。

至于循环法,我的代码工作正常。

对于故障安全的事情,我试图将一个IP更改为某个无效的ip(没有运行任何服务),然后它应该连接到其他IP。但它正在挂断电话:

client = new Socket(ip, port);

可以为此做些什么?如果我没有在2秒内获得连接,我不想重试(如果这是java的功能,我可以减少重试超时吗?)。

此外,经过很长一段时间,它会抛出IllegalStateException(我根本没想到)

这是我的Connect.java构造函数:

//Variable declartion

private static int balancer = 0;
private static boolean[] down = {false, false};
private static int[] countDown = {0, 0};
private static final int maxRequests = 10000;
private static int countRequests = 0;

//Constructor
public Connect() throws NullPointerException{
    int use = balancer;
    int flag = 0;

    countRequests = (countRequests + 1) % maxRequests;
    if(countRequests == 0){
        down[0] = down[1] = false;
    }

    if((balancer == 0 && down[0] == false) || down[1] == true){
        ip = Config.IP1;
        port = Config.PORT1;
        use = 0;
        System.out.println("balancer = 0");
    }
    else{
        ip = Config.IP2;
        port = Config.PORT2;
        use = 1;
        System.out.println("balancer = 1");
    }
     LOGGER.setLevel(Level.INFO);
     // Single simple socket 
     try {
        client = new Socket(ip, port);
        System.out.println("client");
        dos = new DataOutputStream(client.getOutputStream());
        dis = new DataInputStream(client.getInputStream());
        if(client == null || dos == null || dis == null){
            throw new IOException("Connection could not be opened properly\n");
        }
        LOGGER.log(Level.INFO, "Connection to : " + ip + " Successfully opened");
        balancer = (balancer == 0) ? 1 : 0;
        countDown[use] = 0;
        flag = 1;
     } catch (IOException e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
        countDown[use] ++;
        if(countDown[use] >= 3){
            down[use] = true;
        }
        LOGGER.log(Level.SEVERE, ": " + ip + " " + e.getMessage());
     }
     finally{       //if connection was not opened on first 
         if(flag == 0){
             if(use == 0){
                 ip = Config.IP2;
                 port = Config.PORT2;
                 use = 1;
             }
             else{
                 ip = Config.IP1;
                 port = Config.PORT1;
                 use = 0;
             }
             try {
                    client = new Socket(ip, port);
                    dos = new DataOutputStream(client.getOutputStream());
                    dis = new DataInputStream(client.getInputStream());
                    if(client == null || dos == null || dis == null){
                        throw new IOException("Connection could not be opened properly\n");
                    }
                    LOGGER.log(Level.INFO, "Connection to  " + ip + " Successfully opened");
                    balancer = (balancer == 0) ? 1 : 0;
                    countDown[use] = 0;
                    flag = 1;
                 } catch (IOException e) {
                    // TODO Auto-generated catch block
                    //e.printStackTrace();
                    countDown[use] ++;
                    if(countDown[use] >= 3){
                        down[use] = true;
                    }
                    LOGGER.log(Level.SEVERE, ": " + ip + " " + e.getMessage());
                    throw new NullPointerException("Connection could not be opened properly on both machines\n");
                 }
         }
     }
}

我期望的是IO/NullPointer-Exception然后catch块将被执行

1 个答案:

答案 0 :(得分:0)

如何使用默认的Socket()构造函数,然后connect(SocketAddress, int),它允许您指定特定的超时?

示例:

import java.net.*;
public class Test {
  public static void main(String[] args) throws Exception {
    Socket socket = new Socket();
    socket.connect(new InetSocketAddress(args[0], 80), 2000);
  }
}

您可以通过计时来查看超时:

$ time java Test google.com

real    0m0.154s
user    0m0.079s
sys     0m0.022s

$ time java Test 192.168.2.123
Exception in thread "main" java.net.SocketTimeoutException: connect timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at Test.main(Test.java:5)

real    0m2.111s
user    0m0.076s
sys     0m0.026s