Google Answers Logo
View Question
 
Q: Java socket hangs program waiting for message (use RMI instead?) ( No Answer,   2 Comments )
Question  
Subject: Java socket hangs program waiting for message (use RMI instead?)
Category: Computers > Programming
Asked by: timbo70-ga
List Price: $50.00
Posted: 25 Jan 2005 11:31 PST
Expires: 30 Jan 2005 15:32 PST
Question ID: 463146
I have a java program which consists of only an infinite while(true)
loop.  I need to communicate with this program using sockets (or RMI,
or whatever is the right way to do it - please advise!).

I tried the standard socket syntax as below, but as you might guess,
the loop freezes while it waits for a command from the socket.  I need
it to continue its loop w/ NO loss of performance if there is no
socket message sent to it.

Main program:

public static void main(String[] args) {
  while (true) { // whole program is just infinite loop
    ServerSocket serverSocket = null;
    try { serverSocket = new ServerSocket(1234); }
    catch (IOException e) {}

    try {
      Socket clientSocket = serverSocket.accept();
      BufferedReader is = new BufferedReader(new
      InputStreamReader(clientSocket.getInputStream()));
      PrintStream os = new PrintStream(clientSocket.getOutputStream());
      String lineIn = is.readLine();
      String response = processMsg(lineIn);
      os.println(response);
      os.flush();
    }
    catch (IOException e) {}

    otherProcessing(); 
  }
}

Communicator program:

public class Talk {
  public static void main(String[] args) {
  Socket socket = null;
  InputStreamReader isr = null;
  BufferedReader in = null;
  PrintWriter out = null;

  try {
    socket = new Socket("localhost", 1234);
    isr = new InputStreamReader(socket.getInputStream());
    in = new BufferedReader(isr);
    out = new PrintWriter(socket.getOutputStream(), true);
  }
  catch (Exception e) {}

  if (socket!=null && isr!=null && in!=null && out!=null) {
    try {
      out.println(args[0]);
      out.flush();
      out.close();
      in.close();
      socket.close();
    }
    catch (Exception e) {}
  }
}

So when this is complete, i will type 'java Talk command1',
and it will pass command1 on to the main program thru the socket/RMI. 
And if there is no command sent from Talk, the main program will just
keep looping.

Please - if you answer this question, I expect the full completed
code, not just pointers to the solution.  Thank you in advance, -t

Clarification of Question by timbo70-ga on 30 Jan 2005 15:32 PST
Thank you guys, no one seems to have grabbed this so I'll close it and
study up on communicating in a new thread.
Answer  
There is no answer at this time.

Comments  
Subject: Re: Java socket hangs program waiting for message (use RMI instead?)
From: feniks-ga on 28 Jan 2005 03:47 PST
 
try moving your socket creation out of the loop.
Like :
public class Server {

    public static void main(String[] inArgs) throws Exception {
        <b>ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(1234);
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }
        System.out.println("Server started... ");</b>

        while (true) { // whole program is just infinite loop
         ....
         }

Seems to work fine to me.
Subject: Re: Java socket hangs program waiting for message (use RMI instead?)
From: akash_kava-ga on 30 Jan 2005 02:15 PST
 
This is wrong way of programming, 

After calling Accept method of serversocket, when you have recieved
Socket, you must create a new thread and let that new thread handle
the communication.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy