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:
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();
}
}
}
相关推荐
Commons Net是Apache软件基金会开发的一个Java库,主要专注于网络协议,特别是FTP(文件传输协议)相关的功能。"commons-net-3.8.0-bin.tar.gz"是一个包含该库的源码或二进制版本的压缩文件,以.tar.gz格式打包,这...
Transaction Timeouts 525 Updating Multiple Databases 526 Transactions in Web Components 528 Further Information about Transactions 528 Chapter 28: Resource Connections 529 Resources and JNDI ...
SOX001109700040 – INF: Queries with PREFETCH in the plan hold lock until the end of transaction Locking Concepts Delivery Tip Prior to delivering this material, test the class to see if they ...
资源分类:Python库 所属语言:Python 资源全名:pytest-timeouts-1.2.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
the-ultimate-guide-to-ruby-timeouts, 流行 ruby Gems的超时 ruby-超时的终极指南没有反应的服务可能比向下的服务更差。 如果不正确处理,它可以捆绑整个系统。 所有网络请求超时。 下面是为流行 ruby Gems 添加...
If you want a plugin that supports request customization for REST, WebSocket, Socket.IO, SignalR, SignalR Core, Server-Sent Events (and much more) out of the box, with regular updates and outstanding ...
The Way to Go,: A Thorough Introduction to the Go Programming Language 英文书籍,已Cross the wall,从Google获得书中源代码,分享一下。喜欢请购买正版。 目录如下: Contents Preface......................
Fix TLS error handling to avoid connection drops on timeouts (#11563) Fix runtime changes to cluster-announce-*-port to take effect on the local node too (#10745) Fix sentinel function that compares ...
Mitigate network attacks using shunning, anti-spoofing, connection limits, and timeouts Examine network design, management, and troubleshooting best practices This security book is part of the Cisco ...
*) Bugfix: in the stream module timeouts waiting for UDP datagrams from upstream servers were not logged or logged at the "info" level instead of "error". *) Bugfix: when using ...
- **Handling Timeout**: Provides strategies for dealing with timeouts during the execution of scripts, ensuring robustness and reliability. - **Backslashes**: Explains the significance of backslashes ...
Create robust and scalable applications along with responsive UI using concurrency and the multi-threading infrastructure in .NET and C# About This Book Learn to combine your asynchronous operations ...
7. **超时设置(Timeouts)**:包括连接超时、读取超时等,定义了在尝试建立连接或等待响应时的最大等待时间。 8. **SSL设置(SSL Configuration)**:如果启用,可以提供加密的数据传输,增加安全性。 9. **字符...
channels of channels, timeouts, and the select keyword. Then it discusses issues related to shared memory and mutexes before presenting two more Go versions of the wc(1) utility that use channels and ...