如何创建TelnetClient连接池

时间:2017-04-24 16:07:55

标签: java sockets connection inputstream connection-pooling

  

块引用

我正在尝试为TelnetClient连接实现cusotm连接池,但是遇到了一些挑战......

第1步:我已成功创建连接 第2步:我尝试使用上面的连接执行命令 顺利 第3步:但与我尝试执行另一个命令的连接相同 我得到的回应(第2步和第3步)响应和它的反应 似乎我使用相同的连接,我没有关闭它所以数据
存在于连接输入和输入中输出流。

可以重置inputSteam的连接,这样我就可以了 当前命令resposne in it。

TelnetClient连接池是否还有其他开源API?

我在网络和套接字编程方面没有太多经验任何帮助 非常感谢!!

  

块引用

 //Method is used to create a connection 
 private TelnetClient createTelnetConnection() throws SocketException, 
IOException {
    TelnetClient telnetConnection = new TelnetClient();
    telnetConnection.connect(127.0.0.1, 6300);
    telnetConnection.setConnectTimeout(240000);
    telnetConnection.setSoTimeout(300000);
    return telnetConnection;

}

//This method is used to execute command using connection
public String executeCommand(String command, TelnetClient connection) throws InterruptedException,
    IOException {
    OutputStream outputstream = null;
    InputStream inputstream = null;
    String response = "";

    outputstream = connection.getOutputStream();
    outputstream.write(command.getBytes());
    outputstream.flush();

    inputstream = connection.getInputStream();

    response = getStringFromInputStream(inputstream);
    return response;

}

  private String getStringFromInputStream(InputStream inputStream) throws 
   IOException {
      BufferedReader bufferReader = null;
      StringBuilder responseString = new StringBuilder();
      String line;
      bufferReader = new BufferedReader(new InputStreamReader(inputStream));
      String response = null;
      while ((line = bufferReader.readLine()) != null) {
        responseString.append(line);
        break;

    }
    return responseString.toString();
 }

0 个答案:

没有答案