gpt4 book ai didi

java - 什么时候停止读取 telnet 输入?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:03 25 4
gpt4 key购买 nike

目前我有四个类(class) basic MUD client : WeatherDriver 用于主类,LineReader 用于处理 InputStreamLineParser 用于解析 String 的队列,而Connection 包含Apache telnet connection .这是基于 Apache Weather Telnet example .

LineReader 如何知道何时停止读取 InputStream 以向 WeatherDriver 发送消息以开始解析?

线阅读器:

package teln;

import static java.lang.System.out;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class LineReader {

private String prompt = "/[#]/";

public LineReader() {
}

public Queue<String> readInputStream(InputStream inputStream) throws IOException {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(inputStreamReader);
String line = br.readLine();

Queue<String> lines = new LinkedList<>();
while (line != null) { //never terminates...
sb.append(line);
line = br.readLine();
lines.add(line);
}
out.println(lines);
return lines;
}

public void setPrompt(String prompt) {
this.prompt = prompt; //need to determine EOL somehow...
}
}

连接:

package teln;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.SocketException;
import org.apache.commons.net.telnet.TelnetClient;

public class Connection {

private TelnetClient tc = new TelnetClient();

public Connection() {
}

public Connection(InetAddress h, int p, String prompt) throws SocketException, IOException {
tc.connect(h, p);
}

public InputStream getInputStream() {
return tc.getInputStream();
}
}

天气驱动器:

package teln;

import static java.lang.System.out;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.Queue;

public final class WeatherDriver {

private static Connection c;
private static LineReader lineReader = new LineReader();
private static LineParser lineParser = new LineParser();

public static void main(String[] args) throws UnknownHostException, SocketException, IOException {
Properties props = PropertiesReader.getProps();
InetAddress host = InetAddress.getByName(props.getProperty("host"));
int port = Integer.parseInt(props.getProperty("port"));
String prompt = props.getProperty("prompt");
out.println("connecting...");
c = new Connection(host, port, prompt);
InputStream inputStream = c.getInputStream();
out.println("got stream");
Queue<String> lines = lineReader.readInputStream(inputStream);
out.println("got lines");
lineParser.parseLines(lines);
out.println("parsed lines");
}
}

最佳答案

您的线路阅读器必须充分了解“协议(protocol)”,以检测控制终端的程序何时需要输入。 IE。必须有某种提示表明“线路转向”。当它检测到这一点时,它会停止读取并让您的前端执行下一步操作。

如果远程系统有不同的方式来指示它正在等待输入(不同类型的提示)并且如果您需要检测超时条件并采取一些特殊操作,这将变得复杂。

您可能会受益于绘制状态图,显示远程程序可能处于的各种状态,以及如何通过程序的输出将状态转换到 telnet session 。

关于java - 什么时候停止读取 telnet 输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18497656/

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