打开后,Java TelnetClient InputStream关闭

时间:2012-08-27 20:00:58

标签: java inputstream telnet

我正在使用Apache Commons TelnetClient为某些交换机创建一个自动telnet接口。如果我直接从机器telnet到交换机,连接似乎永远不会超时。随机地,在Java程序中,连接似乎立即与InputStream一起关闭。我试图构建一个检查连接失败的检查,并尝试再次建立连接,但如果它第一次失败,它总是失败。

import org.apache.commons.net.telnet.TelnetClient;

  public String connect()
  {
     String errorMessage = null;
     tcConnectionHandle = new TelnetClient();
     tcConnectionHandle.setDefaultTimeout(iTimeOutMilliseconds);

     try
     {
        tcConnectionHandle.connect(strConnectionIP, intConnectionPort);
        osOutput = tcConnectionHandle.getOutputStream();
        isInput = tcConnectionHandle.getInputStream();
        int availableBytes = isInput.available();

        while(availableBytes <= 0)
        {
           tcConnectionHandle = null;
           isInput = null;
           osOutput = null;
           Thread.sleep(500);
           tcConnectionHandle = new TelnetClient();
           Thread.sleep(500);
           tcConnectionHandle.setDefaultTimeout(iTimeOutMilliseconds);
           Thread.sleep(500);
           tcConnectionHandle.connect(strConnectionIP, intConnectionPort);
           Thread.sleep(500);
           osOutput = tcConnectionHandle.getOutputStream();
           Thread.sleep(500);
           isInput = tcConnectionHandle.getInputStream();
           Thread.sleep(500);
           availableBytes = isInput.available();
           System.out.println("reopened: " + availableBytes);
        }
     }
     catch(InterruptedException iX)
     {
        errorMessage = "Could not establish connection. " + iX.getLocalizedMessage();
     }
     catch(SocketException sX)
     {
        errorMessage = sX.getMessage();
     }
     catch(IOException ioX)
     {
        errorMessage = ioX.getMessage();
     }

     return errorMessage;
  }

如果我遗漏Thread.sleep(500),它将永远不会有任何可用的字符。暂停时,它的结果为20,但是,如果我尝试使用isInput.read(),它将返回-1,这意味着InputStream已关闭。

我正在寻找一种方法来捕获连接失败并再次尝试连接。它经常发生,不能再试一次。

1 个答案:

答案 0 :(得分:1)

我认为附加到TelnetClient的InputStream是随机处于关闭状态的。即使我创建了新的TelnetClient对象并在以后重新连接到同一个Telnet服务器,任何在该状态下读取它的尝试都会导致通信失败。这没有意义,但我决定尝试一种新方法,而不是弄清楚TelnetClient类中发生了什么。

我通过在此课程中使用implements TelnetInputListener解决了这个问题。调用telnetInputAvailable()时,InputStream偶尔会为null,但我现在可以通过不对该函数的特定调用执行任何操作来从中恢复。

public String connect()
{
   String errorMessage = null;
   tcConnectionHandle = new TelnetClient();
   tcConnectionHandle.setDefaultTimeout(iTimeOutMilliseconds);
   tcConnectionHandle.registerInputListener(this);

   try
   {
      tcConnectionHandle.connect(strConnectionIP, intConnectionPort);
      osOutput = tcConnectionHandle.getOutputStream();
      isInput = tcConnectionHandle.getInputStream();
   }
   catch(SocketException sX)
   {
      errorMessage = sX.getMessage();
   }
   catch(IOException ioX)
   {
      errorMessage = ioX.getMessage();
   }

   return errorMessage;
}

public Matcher waitForRegularExpression(String regularExpression)
{
   Matcher matcher;
   Pattern pattern = Pattern.compile("(?s)" + regularExpression);
   StringBuilder warningLog = new StringBuilder();

   synchronized(sbInputBuffer)
   {
      matcher = pattern.matcher(sbInputBuffer.toString());

      while(!matcher.find())
      {
         try
         {
            int inputBufferSize = sbInputBuffer.length();
            sbInputBuffer.wait(iTimeOutMilliseconds);

            if(inputBufferSize == sbInputBuffer.length())
            {
               warningLog.append("Did not find pattern and no new input.");
               logWarning(warningLog.toString());
               return null;
            }
         }
         catch(InterruptedException intX)
         {
            warningLog.append("Interrupted waiting on input. ").append(intX.getLocalizedMessage());
         }

         matcher = pattern.matcher(sbInputBuffer.toString());
      }

      sbInputBuffer.delete(0, matcher.end()-1);
   }

   if(!warningLog.toString().isEmpty())
   {
      logWarning(warningLog.toString());
   }

   return matcher;
}

@Override
public void telnetInputAvailable()
{
   synchronized(sbInputBuffer)
   {
      StringBuilder warningLog = new StringBuilder();
      int readBytes = -2;

      if(isInput != null)
      {
         try
         {
            readBytes = isInput.read();

            if(readBytes > 0)
            {
               sbInputBuffer.append((char)readBytes);
            }

            sbInputBuffer.notify();
         }
         catch(IOException ioX)
         {
            warningLog.append("Failed for IO: ").append(ioX.getLocalizedMessage()).append(" - input so far: ")
               .append(sbInputBuffer.toString()).append("\nRead bytes: ").append(readBytes).append("\n");
            logWarning(warningLog.toString());
         }
      }
   }
}