Connection timeouts with the Apache commons TelnetClient

本文介绍如何为Apache Commons Telnet客户端设置连接超时。通过实现自定义SocketFactory,确保连接请求能够按预期超时。提供了一个包含自定义SocketFactory的示例代码。

Connection timeouts with the Apache commons TelnetClient

<!-- .entry-meta -->

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: java, apache, commons

<!-- Social Bookmarks BEGIN -->
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值