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();
      }
   }
}
3
  • 2
    remove those breaks from catch blocks. Commented Jul 27, 2013 at 8:50
  • are u getting exception Commented Jul 27, 2013 at 8:51
  • 1
    You might be getting some Exception and breaking out of loop in catch ! Commented Jul 27, 2013 at 8:51

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

Remove the break statement from catch block

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.