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

时间:2013-08-28 20:32:54

标签: java oop client-server structure telnet

目前我有四个课程用于basic MUD clientWeatherDriver用于主要课程,LineReader用于处理InputStreamLineParser来解析Queue的{​​{1}},而String拥有Apache telnet connection。这基于Apache Weather Telnet example

Connection如何知道何时停止阅读LineReaderInputStream发送邮件以开始解析?

LineReader:

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...
    }
}

WeatherDriver:

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

2 个答案:

答案 0 :(得分:1)

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

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

您可以从绘制状态图中受益,该状态图显示远程程序可以处于的各种状态以及程序输出如何通过telnet会话传递从状态到状态的转换。

答案 1 :(得分:0)

Took a page from Pearson,以下每个字符(我认为)。这是printToConsole方法。

package teln;

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

public class Connection {

    private TelnetClient tc = new TelnetClient();
    private boolean isl = false;
    private String u;
    private String pw;
    //private StreamReader sr;
    private InputStream in;
    private PrintStream out;

    private Connection() {
    }

    public Connection(InetAddress h, int p, String prompt, String u, String pw) throws SocketException, IOException, InterruptedException {
        tc.connect(h, p);
        this.u = u;
        this.pw = pw;
        in = tc.getInputStream();
        out = new PrintStream(tc.getOutputStream());
        printToConsole();
    }

    public void printToConsole() throws IOException  {
        char ch = (char) in.read();
        while (true) {
            System.out.print(ch);
            ch = (char) in.read();
        }
    }

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

    void cmd(String s) throws IllegalArgumentException, IOException {
        byte[] by = s.getBytes();
        for (Byte b : by) {
            tc.sendCommand(b);
        }
    }
}