commons.net.telnet使用示例
2016-04-15 17:59
597 查看
import org.apache.commons.net.telnet.TelnetClient; import java.io.IOException; public class TelnetDemo { public static void main(String[] args) throws IOException { TelnetClient telnet = new TelnetClient(); String remoteip = "10.1.1.159"; int remoteport = 9999; telnet.connect(remoteip, remoteport); System.out.println(telnet.isAvailable()); System.out.println(telnet.isConnected()); IOUtil.readWrite(telnet.getInputStream(), telnet.getOutputStream(), System.in, System.out); telnet.disconnect(); System.exit(0); } }
import org.apache.commons.net.io.Util; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * This is a utility class providing a reader/writer capability required * by the weatherTelnet, rexec, rshell, and rlogin example programs. * The only point of the class is to hold the static method readWrite * which spawns a reader thread and a writer thread. The reader thread * reads from a local input source (presumably stdin) and writes the * data to a remote output destination. The writer thread reads from * a remote input source and writes to a local output destination. * The threads terminate when the remote input source closes. * * */ public final class IOUtil { public static final void readWrite(final InputStream remoteInput, final OutputStream remoteOutput, final InputStream localInput, final OutputStream localOutput) { Thread reader, writer; reader = new Thread() { @Override public void run() { int ch; try { while (!interrupted() && (ch = localInput.read()) != -1) { remoteOutput.write(ch); remoteOutput.flush(); } } catch (IOException e) { e.printStackTrace(); } } }; writer = new Thread() { @Override public void run() { try { Util.copyStream(remoteInput, localOutput); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } }; writer.setPriority(Thread.currentThread().getPriority() + 1); writer.start(); reader.setDaemon(true); reader.start(); try { writer.join(); reader.interrupt(); } catch (InterruptedException e) { e.printStackTrace(); } } }
相关文章推荐
- Redis源代码分析(三十三)--- redis-cli.cclient命令行接口的实现(2)
- cast()、decimal(M,D) --SQL对查询字段保留小数位操作
- javascript 代码alert([] ==![]);为什么弹出结果为true?
- PS教程!手把手教你打造复古几何无缝纹理背景
- 基于Activiti5工作流实战企业协同OA办公系统(spring-data-jpa、uur前台组件)
- Tomcat之虚拟主机配置以及web应用配置
- Ubuntu 14.04LTS 下安装.net框架
- [转] cordova-plugin-x-toast
- 演示:纯CSS实现自适应布局表格
- C#时间格式化(Datetime)用法详解
- HTML---CSS
- solr 搜索引擎
- 设置 label 的行间距和删除线
- 图片和多行文本的垂直居中
- redis分布式锁的实现
- java持久层框架mybatis如何防止sql注入
- poj 3244 Difference between Triplets 最值公式推导
- oj--C++结构体之统计最高最低分
- easy touch利用playmaker拖动ngui对象
- 科目二难点——倒车入库