简体   繁体   中英

Connection timeout occurs while connecting through telnet

I am writing a telnetclient example using apache-commons-net.2.0.jar to connect to a target using telnet protocol. There is no problem with the jar it establishes the connection with the host if the host is up and running. If i reboot the system and tries to connect to the host telnetclient is throwing Connection time out Exception.

telnetClient.setConnectionTimoeout(180 * 1000) --> setting 180 seconds connection timeout telnetClient.setDefaultTimoeout(180 * 1000)

Telnet client is not waiting upto 180 seconds. It throws connection timeout exception after 30 seconds(approximately). I tried the example with commons-net-3.0.jar and getting the same exception. As per my understanding setConnectionTimeout should wait upto 180 seconds if the host is not up.

  import java.io.IOException;
import org.apache.commons.net.telnet.TelnetClient;
import java.net.*;

class TelnetClientExample
{
    public static void main(String[] args)
    {
    System.out.println("Started");
        TelnetClient telnet;
        telnet = new TelnetClient();
        try
        {
            telnet.setConnectTimeout(1800000);
        telnet.setDefaultTimeout(1800000);
        telnet.connect(InetAddress.getByName("hostname"),23);
            //telnet.connect("ipaddress", 23);
        System.out.println("Connected");

        }
        catch (IOException e)
        {
            e.printStackTrace();
        System.out.println("Connection timeout = "+telnet.getConnectTimeout());
            System.exit(1);
        }
    try
        {
            telnet.disconnect();
        }
        catch (IOException e)
        {
            e.printStackTrace();
            System.exit(1);
        }
        System.exit(0);
    }
}

Error:

Started
java.net.ConnectException: Connection timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at org.apache.commons.net.SocketClient.connect(SocketClient.java:182)
    at TelnetClientExample.main(TelnetClientExample.java:17)

Thanks in Advance..

You can't increase a TCP connection timeout beyond the platform default (of about a minute) by any means. You can only decrease it. Contrary to what the Javadoc says in several places, a zero connect timeout doesn't mean infinity, it means the platform default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM