gpt4 book ai didi

Java Telnet 库

转载 作者:行者123 更新时间:2023-12-02 19:56:20 24 4
gpt4 key购买 nike

我真的不清楚解释这个要求,但我基本上需要的是一个连接到 Unix 服务器并获取文件字数并显示在 JSP 页面上的 JSP 页面。我在这里查看了各种问题,但没有任何帮助。示例代码会有很大帮助。谢谢

最佳答案

Kavin,我想您现在一定已经找到了其他解决方案或继续前进。但是,我刚刚遇到一个要求,导致我进入此页面。

我浏览了这个页面和许多其他页面上有些愚蠢的回复,但根本找不到一个简单易用的 Telnet 客户端。

我花了一点时间,在 Commons Net 的解决方案之上编写了一个简单的客户端。请原谅代码中的 System.out 和 System.err,我几乎无法正常工作。

public static void main(String[] args) throws Exception {
SimpleTelnetClient client = new SimpleTelnetClient("localhost", 2323);
client.connect();

String result = client.waitFor("login:");
System.out.println("Got " + result);
client.send("username");
result = client.waitFor("Password:");
System.out.println("Got " + result);
client.send("password");
client.waitFor("#");
client.send("ls -al");
result = client.waitFor("#");
System.out.println("Got " + result);
client.send("exit");
}

不确定它是否会对您有帮助,但也许它可以作为其他人的起点。

import java.io.InputStream;
import java.io.PrintStream;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.commons.net.telnet.EchoOptionHandler;
import org.apache.commons.net.telnet.InvalidTelnetOptionException;
import org.apache.commons.net.telnet.SuppressGAOptionHandler;
import org.apache.commons.net.telnet.TelnetClient;
import org.apache.commons.net.telnet.TerminalTypeOptionHandler;

public class SimpleTelnetClient {
static class Responder extends Thread {
private StringBuilder builder = new StringBuilder();
private final SimpleTelnetClient checker;
private CountDownLatch latch;
private String waitFor = null;
private boolean isKeepRunning = true;

Responder(SimpleTelnetClient checker) {
this.checker = checker;
}

boolean foundWaitFor(String waitFor) {
return builder.toString().contains(waitFor);
}

public synchronized String getAndClearBuffer() {
String result = builder.toString();
builder = new StringBuilder();
return result;
}

@Override
public void run() {
while (isKeepRunning) {
String s;

try {
s = checker.messageQueue.take();
} catch (InterruptedException e) {
break;
}

synchronized (Responder.class) {
builder.append(s);
}

if (waitFor != null && latch != null && foundWaitFor(waitFor)) {
latch.countDown();
}
}
}

public String waitFor(String waitFor) {
synchronized (Responder.class) {
if (foundWaitFor(waitFor)) {
return getAndClearBuffer();
}
}

this.waitFor = waitFor;
latch = new CountDownLatch(1);
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
return null;
}

String result = null;
synchronized (Responder.class) {
result = builder.toString();
builder = new StringBuilder();
}

return result;
}
}

static class TelnetReader extends Thread {
private final SimpleTelnetClient checker;
private final TelnetClient tc;

TelnetReader(SimpleTelnetClient checker, TelnetClient tc) {
this.checker = checker;
this.tc = tc;
}

@Override
public void run() {
InputStream instr = tc.getInputStream();

try {
byte[] buff = new byte[1024];
int ret_read = 0;

do {
ret_read = instr.read(buff);
if (ret_read > 0) {
checker.sendForResponse(new String(buff, 0, ret_read));
}
} while (ret_read >= 0);
} catch (Exception e) {
System.err.println("Exception while reading socket:" + e.getMessage());
}

try {
tc.disconnect();
checker.stop();
System.out.println("Disconnected.");
} catch (Exception e) {
System.err.println("Exception while closing telnet:" + e.getMessage());
}
}
}

private String host;
private BlockingQueue<String> messageQueue = new LinkedBlockingQueue<String>();
private int port;
private TelnetReader reader;
private Responder responder;
private TelnetClient tc;

public SimpleTelnetClient(String host, int port) {
this.host = host;
this.port = port;
}

protected void stop() {
responder.isKeepRunning = false;
responder.interrupt();
}

public void send(String command) {
PrintStream ps = new PrintStream(tc.getOutputStream());
ps.println(command);
ps.flush();
}

public void sendForResponse(String s) {
messageQueue.add(s);
}

public void connect() throws Exception {
tc = new TelnetClient();

TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler("VT100", false, false, true, false);
EchoOptionHandler echoopt = new EchoOptionHandler(true, false, true, false);
SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true, true, true, true);

try {
tc.addOptionHandler(ttopt);
tc.addOptionHandler(echoopt);
tc.addOptionHandler(gaopt);
} catch (InvalidTelnetOptionException e) {
System.err.println("Error registering option handlers: " + e.getMessage());
}

tc.connect(host, port);
reader = new TelnetReader(this, tc);
reader.start();

responder = new Responder(this);
responder.start();
}

public String waitFor(String s) {
return responder.waitFor(s);
}
}

关于Java Telnet 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5988029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com