Connection timeouts with the Apache commons TelnetClient

I recently used the Apache commons net package in a project to create a small telnet client that automated a login process. It is hard to find a lot of documentation on TelnetClient but there are some examples. For what I wanted to use the telnet client for I ran into a problem because I needed the connect call to time out. Try as I might I couldn’t get setDefaultTimeout to work as advertised.

As it turns out the Apache commons developers are trying to keep the net commons package compatible with java 1.3 for some reason (see this issue in jira). If you want to have your connect request time out you have to implement your own SocketFactory first. Since there seems to be a lot of confusion on this and the commons net project seems to be idle now I figured it was worth writing about in case other people ever go looking.

Here is an example with a custom SocketFactory that will get the timeouts to work on connect:

[code lang=”java”]
package net.ioncannon;

import org.apache.commons.net.telnet.TelnetClient;
import org.apache.commons.net.SocketFactory;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.net.*;

public class TelAllTestMain
{
public static void main(String[] args) throws IOException
{
TelnetClient telnetClient = new TelnetClient();
telnetClient.setSocketFactory(new TimeoutSockectFactory());
telnetClient.setDefaultTimeout(1000);
telnetClient.connect(“localhost”);
telnetClient.setSoTimeout(1000);
telnetClient.setSoLinger(true, 1000);

BufferedReader reader = new BufferedReader(new InputStreamReader(telnetClient.getInputStream()));

StringBuffer stringBuffer = new StringBuffer();
try
{
char buffer[] = new char[1024];
int size = -1;
while((size = reader.read(buffer)) != -1)
{
stringBuffer.append(buffer, 0, size);
if(stringBuffer.toString().endsWith(“something”))
{
System.err.println(“Found the string…”);
break;
}
}
}
catch (Exception e)
{
System.err.println(“Didn’t find the string…”);
}

telnetClient.disconnect();
}

private static class TimeoutSockectFactory implements SocketFactory
{
public Socket createSocket(String hostname, int port) throws IOException
{
Socket socket = new Socket();
socket.connect(new InetSocketAddress(hostname, port), 1000);
return socket;
}

public Socket createSocket(InetAddress hostAddress, int port) throws IOException
{
Socket socket = new Socket();
socket.connect(new InetSocketAddress(hostAddress, port), 1000);
return socket;
}

public Socket createSocket(String remoteHost, int remotePort, InetAddress localAddress, int localPort) throws IOException
{
return new Socket();
}

public Socket createSocket(InetAddress remoteAddress, int remotePort, InetAddress localAddress, int localPort) throws IOException
{
return new Socket();
}

public ServerSocket createServerSocket(int port) throws IOException
{
return new ServerSocket();
}

public ServerSocket createServerSocket(int port, int backlog) throws IOException
{
return new ServerSocket();
}

public ServerSocket createServerSocket(int port, int backlog, InetAddress bindAddress) throws IOException
{
return new ServerSocket();
}
}
}
[/code]

[tags]java, apache, commons[/tags]

2 thoughts on “Connection timeouts with the Apache commons TelnetClient

  1. Oriol

    Hello Carson,

    I know the post is quite old but as you said, there’s not too much information about Telnet and so I’ve finally found your webpage. I’ll try if you still can help me and if not thanks anyways.

    So I’ve made an application for connecting my computer to a telnet server (using apache common telnetclient) and everything’s working while no exceptions are coming. I mean, as you know,telnet is not sending any end point so for example I keep reading until I match a word. Of course that when this word does not come, my program keeps reading and so I would like to implement a Timeout to unblock it.

    Well, I’ve tried to build your code and Netbeans does not recognise me the “org.apache.commons.net.SocketFactory” but it does for “org.apache.commons.net.telnet.TelnetClient” so I get an error on:

    ****private static class TimeoutSockectFactory implements SocketFactory****

    as it can not find the class “SocketFactory”.

    Do you know if they have changed the package? I went to apache commons and in “org.apache.commons.net.*” it does not appear the SocketFactory package. What do you suggest?

    Thank you,

    Oriol

Comments are closed.