java - 强制 read() 方法读取最少字符数

标签 java telnet bufferedreader expect

我正在尝试使用预期的实现方式对路由器进行远程登录。

我的代码如下,套接字通信如下,

            server = "my-server-ip-domain-here";
            socket = new Socket();
            socket.connect(new InetSocketAddress(server, 23), 10000);//Will wait for 10 seconds
            socket.setKeepAlive(true);
            socket.setSoTimeout(10000);
            expectBuffer = new StringBuilder();
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            pw = new PrintWriter(socket.getOutputStream(), true);

我的发送实现如下,

  public static void send(String cmd) {
        pw.print(cmd + "\r");
        pw.flush();
    }

我的预期实现如下,

  public static String expect(String expectString) {
        try {

            int c = 0;


            char[] buf = new char[4096];

            //Here c will return the no. of chars read 
            while ((c = br.read(buf)) != -1) {


                String tmp = "";

                //converting that char array to String
                for (int i = 0; i < c; i++) {
                    //Printing that character
                    System.out.print(buf[i]);

                    tmp += buf[i];
                }



                expectBuffer.append(tmp).append(NEW_LINE);


                if (expectBuffer.toString().contains(expectString)) {
                    break;
                }
            }
            String expBuff = expectBuffer.toString();
            expectBuffer.setLength(0);
//            System.out.println(expBuff);
            return expBuff;
        } catch (Exception e) {
            System.out.println(e);
            return "";
        }

    }

我面临的问题是没有。 BufferedReader 每次读取的字符数。

即每次我都必须向路由器发送一些命令,并且 BufferedReader 也会读取这些命令。

例如。

send ("commandABC");
expect(prompt);
send ("command-efg");
expect(prompt);
send ("commandHij");
expect(prompt);

根据我发送的命令,它将显示一些输出。无论我发送什么,它也会被阅读,不幸的是,它是以单独的方式打印的。

如下所示。

com
mandABC

<command output here>

command-
efg

<command output here>

commandHij

<command output here>

正如我上面指出的,只有命令(无论我发送什么命令)都会以单独的方式打印。

我已经查过了。当时读取了of char,发现范围是2-10。

这就是为什么它以这种方式打印。

有没有办法限制读取至少 100 个字符?

提前致谢。

最佳答案

  • 如果您想等到读完整行文本,请尝试 bf.readLine() (您需要确保每个命令都以“\n”终止
  • 如果您想确保在继续处理之前已读取一定数量的字符(例如 100 个),请使用循环:

    char buffer[128];
    for (int charsRead = 0; charsRead < 100; ) {
        charsRead += bf.read(buffer, charsRead, (100 - charsRead));
    }
    

    请注意 bf.read() 的(详细)语法:

    bf.read(buffer, offset_size, max_to_read)
    

    通过charsRead因为偏移量大小意味着读取的每个字符 block 将存储在先前读取的字符之后。路过(100 - charsRead)max_to_read限制您阅读的总字符数。

来源:API 引用 http://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html#read(char[],%20int,%20int

关于java - 强制 read() 方法读取最少字符数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17541148/

相关文章:

java - 运行不存在的 jar 不会导致任何异常/错误

unix - 如何向 telnet 发送命令并使 session 保持打开状态

php - 使用php Telnet到cisco交换机

java - 如何在 Java 中存储文本文件字符串以供以后使用

java - netbeans 中的简单 java 票务系统

java - 在文本文件中搜索字符串?

java - 在 Android Studio 中的类上执行 Junit 测试,无需初始化移动设备/模拟器

java - Dropwizard 使用 jakarta 而不是 javax

java - 避免在时间戳上使用 java.lang.ThreadGroup

wcf - netstat 说 443 已打开,但我无法使用 telnet 连接到它。为什么?