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:

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

Tags: , ,

Leave a Reply

Your email address will not be published.