需要帮助改进java客户端端口侦听器 [英] Need help improving a java client side port listener

查看:81
本文介绍了需要帮助改进java客户端端口侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一小段代码在包含SWING控件的applet中运行,用于将信息写入某个端口上的套接字,然后侦听响应。这工作正常,但它有一个问题。端口侦听器基本上处于循环中,直到服务器收到null。我希望用户能够在等待服务器响应的同时在applet实例化的GUI中执行其他操作(这可能需要几分钟才能完成)。我还需要担心服务器和客户端之间的连接断开连接。但是编写代码的方式,applet似乎会冻结(它实际上处于循环中),直到服务器响应。如何允许侦听器在后台进行侦听,允许程序中发生其他事情。我假设我需要使用线程,我确信这个应用程序,它很容易实现,但我缺乏坚实的线程基础阻碍了我。下面是代码(你可以看到它是多么简单)。如何改进它以使其完成我需要做的事情>

I have a small bit of code that runs in an applet that contains SWING controls and is used to write information to a socket on a certain port and then listens for a response. This works fine, but there is a problem with it. The port listener is essentially in a loop until null is received by the server. I want users to be able to perform other actions in the GUI instantiated by the applet while waiting for the server to respond (this could take minutes to occur). I also need to worry about the connection between the server and the client disconnecting. But the way the code is written, the applet appears to freeze (its really in a loop) until the server responds. How can I allow the listener to do its listening in the background, allowing other things to occur in the program. I assume I need to use threads and I'm sure for this application, it is easy to implement, but my lack of a solid thread foundation is hampering me. Below is the code (you can see how simple it is). How can I improve it to make it do what I need it to do>

  public String writePacket(String packet) {
/* This method writes the packet to the port - established earlier */
   System.out.println("writing out this packet->"+packet+"<-");
   out.println(packet);
  String thePacket =    readPacket();  //where the port listener is invoked.  
   return thePacket;
   }
    private String readPacket() {
      String thePacket ="";
      String fromServer="";
      //Below is the loop that freezes everything.   
     try {
      while ((fromServer = in.readLine()) != null) { 
        if (thePacket.equals("")) thePacket = fromServer;
        else 
        thePacket = thePacket+newLine+fromServer;
    }
        return thePacket;  //when this happens, all listening should stop.   
   } catch (IOException e) {
        // TODO Auto-generated catch block
    e.printStackTrace();
    return null;
    }

     }  

谢谢,

Elliott

推荐答案

有很多不同的方法可以在不同的线程上执行IO ,但在这种情况下,你可能想使用 SwingWorker

There lots of different means of getting the IO performed on a different thread, but in this case you probably want to use SwingWorker.

您的代码如下所示:

private final Executor executor = Executors.newSingleThreadExecutor();

public void writePacket(final String packet) 
{
  // schedules execution on the single thread of the executor (so only one background operation can happen at once)
  //
  executor.execute(new SwingWorker<String, Void>()
      {

        @Override
        protected String doInBackground() throws Exception
        {
          // called on a background thread


          /* This method writes the packet to the port - established earlier */
          System.out.println("writing out this packet->"+packet+"<-");
          System.out.println(packet);
          String thePacket = readPacket();  //where the port listener is invoked.  
          return thePacket;            
        }

        @Override
        protected void done()
        {
          // called on the Swing event dispatch thread


          try
          {
            final String thePacket = get();

            // update GUI with 'thePacket'
          }
          catch (final InterruptedException e)
          {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          catch (final ExecutionException e)
          {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } 
        }
      });
}

private String readPacket() 
{
  String thePacket ="";
  String fromServer="";
  //Below is the loop that freezes everything.   
  try 
  {
    while ((fromServer = in.readLine()) != null) 
    { 
      if (thePacket.equals("")) 
        thePacket = fromServer;
      else 
        thePacket = thePacket+newLine+fromServer;
    }
    return thePacket;  //when this happens, all listening should stop.   
  } 
  catch (IOException e) 
  {
    e.printStackTrace();
    return null;
  }
}

这篇关于需要帮助改进java客户端端口侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆