Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么ByteArrayInputStream不';是否返回预期结果?_Java_Telnet - Fatal编程技术网

Java 为什么ByteArrayInputStream不';是否返回预期结果?

Java 为什么ByteArrayInputStream不';是否返回预期结果?,java,telnet,Java,Telnet,我试图通过telnet与windows server中的应用程序交互,所以我使用TelnetClient()方法。我可以使用System.in.read()进行交互(发送命令和检索结果),但是我希望该程序能够自动运行,而不使用任何键盘输入。所以,我的问题是,为什么System.in.read()可以工作,而ByteArrayInputStream不能工作 这是我目前的代码: public class telnetExample2 implements Runnable, TelnetNotifi

我试图通过telnet与windows server中的应用程序交互,所以我使用TelnetClient()方法。我可以使用System.in.read()进行交互(发送命令和检索结果),但是我希望该程序能够自动运行,而不使用任何键盘输入。所以,我的问题是,为什么System.in.read()可以工作,而ByteArrayInputStream不能工作

这是我目前的代码:

public class telnetExample2 implements Runnable, TelnetNotificationHandler{
 static TelnetClient tc = null;
 public static void main (String args[]) throws IOException, InterruptedException{
  tc = new TelnetClient();
   while (true){
    try{
     tc.connect("192.168.1.13", 8999);
     }
     catch (SocketException ex){
      Logger.getLogger(telnetExample2.class.getName()).log(Level.SEVERE, null,ex);
     }
    Thread reader = new Thread(new telnetExample2());
    tc.registerNotifHandler(new telnetExample2());
    String command = "getversion"; //this is the command i would like to write
    OutputStream os = tc.getOutputStream();
    InputStream is = new ByteArrayInputStream(command.getBytes("UTF-8")); //i'm using UTF-8 charset encoding here
    byte[] buff = new byte[1024];
    int ret_read = 0;
    do{
     ret_read = is.read(buff);
     os.write(buff, 0, 10)
     os.flush();
    while(ret_read>=0);
    }
 }

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){
     System.out.print(new String(nuff, 0, ret_read));
    }
   while(ret_read>=0);}
  catch(Exception e){
  System.err.println("Exception while reading socket:" + e.getMessage());
  }
 }

public void receiveNegotiation(int i, int ii){
 throw new UnsupportedOperationException("Not supported");
 }
}
您可以将不起作用的9行代码缩减为os.write(command.getBytes(“UTF-8”)哪个可以


为什么您认为将最多1024个字节读入缓冲区,然后只写出其中的前10个字节是可行的,这是一个谜。

在将命令复制到输出流的代码中,存在一个逻辑错误。写操作系统(buff,0,10)必须是操作系统。写操作系统(buff,0,ret_-read)你没有说你正在观察的东西。请阅读
InputStream is = new ByteArrayInputStream(command.getBytes("UTF-8")); //i'm using UTF-8 charset encoding here
byte[] buff = new byte[1024];
int ret_read = 0;
do{
 ret_read = is.read(buff);
 os.write(buff, 0, 10)
 os.flush();
while(ret_read>=0);
}