博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
commons.net.telnet使用示例
阅读量:6253 次
发布时间:2019-06-22

本文共 2727 字,大约阅读时间需要 9 分钟。

 

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

 

转载于:https://www.cnblogs.com/softidea/p/5396341.html

你可能感兴趣的文章
论坛程序推荐,区别
查看>>
spring mvc redirect 重定向 跳转并传递参数
查看>>
《止学》 [隋]文中子(王通)
查看>>
微信小程序之底部弹框预约插件
查看>>
基础知识 - Golang 中的正则表达式
查看>>
分享一个shell脚本的坑:grep匹配+wc取值 在脚本执行后的结果与手动执行结果不一致...
查看>>
【Clojure 基本知识】 关于函数参数的各种高级用法
查看>>
顺序图【6】--☆☆
查看>>
Docker 版本
查看>>
【ABP杂烩】面向切面编程(AOP)知识总结
查看>>
java 如何使用多线程调用类的静态方法?
查看>>
不能运行VS2005的DSL Tool例子
查看>>
OSI/RM参考模型和TCP/IP协议的关系
查看>>
Android Intent用法汇总
查看>>
使用PIP扩展BTARN
查看>>
GetLastError()返回值及含义
查看>>
android UI之Shape详解_GradientDrawable
查看>>
Oracle Management Server配置
查看>>
O/R Mapping实际开发经验之谈(转)
查看>>
SPSS Clementine 数据挖掘入门 (3)
查看>>