简体   繁体   中英

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?

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. Then find the solution for that exception :)

I think u are getting exception so remove the break; from catch block

从catch块中删除break语句

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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