简体   繁体   English

while循环仅执行一次

[英]while loop is executing only once

I wrote a simple server application that is given below. 我编写了下面提供的简单服务器应用程序。 I have a while loop inside run() but it seems to be doing executing try block only once.I want the program to keep spitting out "GoodBye's" continiously.How do I get it do do that? 我在run()中有一个while循环,但似乎只执行一次try块。我希望程序连续不断吐出“ GoodBye's”。我该怎么做呢?

import java.net.*;
import java.io.*;

public class GreetingServer extends Thread
{
   private ServerSocket serverSocket;

   public GreetingServer() throws IOException
   {
      serverSocket = new ServerSocket(5063);
      serverSocket.setSoTimeout(0);
   }

   public void run()
   {
      while(true)
      {
         try
         {
            System.out.println("Waiting for client on port " +
                        serverSocket.getLocalPort() + "...");

        Socket server = serverSocket.accept();

        System.out.println("Just connected to "
                                + server.getRemoteSocketAddress());

            DataOutputStream out = new DataOutputStream(server.getOutputStream());

            out.writeUTF("\nGoodbye!");
            //server.close();
         }
     catch(SocketTimeoutException s)
         {
            System.out.println("Socket timed out!");
            break;
         }
     catch(IOException e)
         {
            e.printStackTrace();
            break;
         }
     continue;
      }
   }
   public static void main(String [] args)
   {
      try
      {
         Thread t = new GreetingServer();
         t.start();
      }
      catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}

You are surely getting exception check your logcat for exception in eclipse. 您肯定会得到异常检查eclipse中的logcat是否存在异常。 Then find the solution for that exception :) 然后找到该异常的解决方案:)

I think u are getting exception so remove the break; 我认为您正在获得例外,因此请删除休息时间; from catch block 从捕获块

从catch块中删除break语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM