【问题标题】:Force the read() method to read a minimum no of characters强制 read() 方法读取最少字符数
【发布时间】:2013-07-06 15:24:23
【问题描述】:

我正在尝试通过期望的实现方式对路由器进行 telnet。

我的socket通信代码如下,

            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>

正如我上面指出的,只有我发送的命令会以单独的方式打印。

我已经检查了号码。当时读取的char,发现是2-10不等。

这就是它以这种方式打印的原因。

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

提前致谢。

【问题讨论】:

    标签: java telnet bufferedreader expect


    【解决方案1】:
    • 如果您想等到读完整行文本,请尝试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 作为偏移量大小意味着读取的每个字符块将存储在先前读取的字符之后。将 (100 - charsRead) 传递为 max_to_read 会限制您阅读的总字符数。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      • 2019-01-13
      • 2021-11-25
      • 2019-05-11
      • 1970-01-01
      • 2020-08-06
      • 2011-10-23
      相关资源
      最近更新 更多