首页 > 其他分享 >commons.net.telnet使用示例

commons.net.telnet使用示例

时间:2022-12-28 14:34:23浏览次数:39  
标签:示例 writer commons telnet System reader import net public

 

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

}

 



标签:示例,writer,commons,telnet,System,reader,import,net,public
From: https://blog.51cto.com/u_15147537/5974446

相关文章

  • asp.netcore Authentication, schema
    1. code likethiswillreporterror:  builder.Services.AddAuthentication("dd").AddCookie("ddd");builder.Services.AddAuthorization();InvalidOperationE......
  • .net 批量大文件上传下载
    ​IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头。 一. 两个必要响应头Accept-Ranges、ETag      ......
  • .NET(C#、VB)APP开发——Smobiler平台控件介绍:VLCPlay
    本文简述如何在Smobiler中使用VLCPlayer插件,该插件支持播放rtsp流。Step1.新建一个SmobilerForm窗体,再拖入VLCPlay,布局如下在设计器中给VLCPlayer.Url赋值或者在窗......
  • 【开源】基于.net6+gtksharp实现的Linux下的图形界面串口调试工具
    背景参考okeyl.com22年初从上家互联网公司离职以后,充分认识到互联网行业的风险,公司在没有自身稳定产品的情况下,互联网行业就是一个烧钱的行业,支出远远大于收入来......
  • POJ 2531 Network Saboteur(DFS)
    POJ2531NetworkSaboteur题意​ 把n个节点分成AB两组,给出矩阵\(C_{i,j}\),求\(\sum{C_{i,j}}(i\inA,j\inB)\)的最大值。思路​ n很小,直接爆搜做。枚举一下......
  • mxnet 数据操作
     文章目录​​1.创建NDArray​​​​1.1模块导入:​​​​1.2创建行向量:​​​​1.3获取NDArray实例的形状​​​​1.4获取NDArray实例中元素(element)的总数:​​​​1.5......
  • asp.net 批量大文件上传下载
    ​ 以ASP.NETCoreWebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API,包括文件的上传和下载。 准备文件上传的API #region 文件上传......
  • .NET 数值类型
    在C#中,有int16,用short来定义;有int32,用int定义;用int64,用long来定义。在.NET7中,添加了int128,和unint128,位数更大的整型。接下来,我们在.NETConsole测试一下结果:varint16......
  • 第7章 使用Razor视图渲染HTML(ASP.NET Core in Action, 2nd Edition)
    本章包括•创建Razor视图以向用户显示HTML•使用C#和Razor标记语法动态生成HTML•使用布局和局部视图重用公共代码RazorPagesPageModel、页面处理程序和Razor视图中......
  • .NET 云原生架构师训练营(基于 OP Storming 和 Actor 的大型分布式架构一)--学习笔记
    目录为什么我们用OrleansDaprVSOrleansActor模型Orleans的核心概念为什么我们用Orleans分布式系统开发、测试的难度(服务发现、通信)运维的复杂度(伸缩性与可靠性的保障)a......