0

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();
      }
   }
}
4

3 回答 3

2

You are surely getting exception check your logcat for exception in eclipse. Then find the solution for that exception :)

于 2013-07-27T08:54:55.217 回答
1

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

于 2013-07-27T08:52:51.253 回答
0

Remove the break statement from catch block

于 2013-07-27T09:03:56.013 回答