0

I have implemented a socket-based client-server architecture where the server receives requests from the client, does some computation and returns a result. For simplicity, assume that the client sends a request to the server and the server executes a task 100 times returning an array of those results.

I packaged the server as a jar file and I am firing up a new server process within the client (aiming to achieve parallel processing at some point). However, the iterative process freezes at the 80th iteration :S. When I stop the execution the server carries on and completes its task.

I also tried running the server-client using the following options:

  1. As separate Java applications (first the server and then the client)
  2. From the same Java application (starting the server first and then the client)

Both 1 & 2 work fine but the intended way halts at some point.

Do you know what might be the problem?

Update

It seems that server execution freezes when it tries to write to a file (System.err in that case)

Stack trace:
 java.io.FileOutputStream.writeBytes(Native Method)
 java.io.FileOutputStream.write(FileOutputStream.java:326)
 java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
 java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
 java.io.PrintStream.write(PrintStream.java:482)
 sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
 sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
 sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:104)
 java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:185)
 java.io.PrintStream.write(PrintStream.java:527)
 java.io.PrintStream.print(PrintStream.java:669)
 java.io.PrintStream.append(PrintStream.java:1065)
 java.io.PrintStream.append(PrintStream.java:57)
 java.util.Formatter$FixedString.print(Formatter.java:2595)
 java.util.Formatter.format(Formatter.java:2508)
 java.io.PrintStream.format(PrintStream.java:970)
 java.io.PrintStream.printf(PrintStream.java:871)

Update 2

Problem: Buffer overflow on server side

Solutions:

A) Increase Eclipse output console capacity (instructions here).

B) Redirect System.out or System.err to a file.

C) As suggested by @Eashi: disable System.err/out (instructions here).

D) Another good solution by @Peter Lawrey if you use ProcessBuilder: merge System.err with System.out by

    ProcessBuilder pb = new ProcessBuilder(....);         
    pb.redirectErrorStream(true);
Community
  • 1
  • 1
STiGMa
  • 906
  • 3
  • 11
  • 24
  • What is the threads doing when the program halts? What do you see in your debugger or when you take a stack dump? – Peter Lawrey Feb 22 '16 at 12:49
  • @PeterLawrey Just added the stack trace of main thread. Could this problem occur because of a full buffer? I am using a library that writes into system err... – STiGMa Feb 22 '16 at 13:09
  • If the System.err buffer fills the program will stop. You need to ensure it is being read e.g. if you spawn it as a `Process` – Peter Lawrey Feb 22 '16 at 13:22
  • @PeterLawrey, I've just checked this and you are correct. I redirected System.err to a text file and it works OK. Any ideas how to increase the buffer or clear System.err? – STiGMa Feb 22 '16 at 13:28
  • 1
    quick google gives this: http://stackoverflow.com/questions/5936562/disable-system-err alternatively you could just route it to /dev/null – Eashi Feb 22 '16 at 13:58
  • @STiGMa It is better to read System.err as it might print error messages which can tell you when something stops working, why it stopped working. If you use ProcessBuilder you can use `pb.redirectErrorStream(true);` to merge System.err with System.out to make it easier to read. – Peter Lawrey Feb 22 '16 at 14:37

0 Answers0