需要帮助改进java客户端端口侦听器吗

需要帮助改进java客户端端口侦听器吗,java,multithreading,sockets,Java,Multithreading,Sockets,我有一小段代码,运行在包含SWING控件的小程序中,用于将信息写入某个端口上的套接字,然后侦听响应。这很好,但有一个问题。端口侦听器基本上处于循环中,直到服务器接收到null。我希望用户能够在等待服务器响应时,在applet实例化的GUI中执行其他操作(这可能需要几分钟的时间)。我还需要担心服务器和客户端之间的连接断开。但是,按照编写代码的方式,小程序在服务器响应之前似乎是冻结的(实际上是在一个循环中)。如何允许侦听器在后台进行侦听,允许程序中发生其他事情。我假设我需要使用线程,我确信对于这个应

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

  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;
    }

     }  
公共字符串写包(字符串包){
/*此方法将数据包写入先前建立的端口*/

System.out.println(“写出这个包->”+packet+“所有网络I/O都应该在一个单独的线程中


BTW readLine()在服务器关闭连接时返回null,而不是在服务器暂时完成数据发送时返回null。

在不同的线程上执行IO有很多不同的方法,但在这种情况下,您可能需要使用

您的代码看起来像:

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;
  }
}
private final Executor Executor=Executors.newSingleThreadExecutor();
公共无效写包(最终字符串包)
{
//计划在执行器的单个线程上执行(因此一次只能执行一个后台操作)
//
executor.execute(新SwingWorker()
{
@凌驾
受保护的字符串doInBackground()引发异常
{
//在后台线程上调用
/*此方法将数据包写入先前建立的端口*/

在实践中,“out”.pLtLn(“写出这个包->”+包+“java并发”是一个很好的读来增强线程基础;除了正确的之外,还有一个用于socket连接的线程池。