Network communication hangs
I am writing a program where I am collecting data via a Runtime function as in:
Code :
rt = Runtime.getRuntime();
String cmd;
cmd = " vmstat 1 2 ";
do {
try {
proc = rt.exec(cmd);
in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while ((fromUser = in.readLine()) != null) {
try {
out.println(hostname +" vmstat " + "Y_ "+fromUser + " _Z " + now());
fromRuser = rin.readLine(); // Ack from server
} catch (SocketException e) {
e.printStackTrace();
System.err.println("Server host socket disconnect --");
}
}
out.println("Y_CC "+hostname+" End VMSTAT _Z " + now()); //
int exitVal = proc.waitFor();
System.out.println("Process -vmstat- exitValue: " + exitVal);
} catch (Throwable t){
t.printStackTrace();
}
}while (true);
.....
This isn't the entire program but it seems to run fine, for several iterations then hangs, after which I can do a kill -QUIT nnnn and obtain a dump file that looks like
Code :
java.lang.Thread.State: RUNNABLE
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:272)
at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:276)
at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:122)
- locked <0xc95640b8> (a java.io.OutputStreamWriter)
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:212)
at java.io.BufferedWriter.flush(BufferedWriter.java:236)
- locked <0xc95640b8> (a java.io.OutputStreamWriter)
at java.io.PrintWriter.newLine(PrintWriter.java:438)
- locked <0xc9553ae0> (a java.io.BufferedWriter)
at java.io.PrintWriter.println(PrintWriter.java:585)
at java.io.PrintWriter.println(PrintWriter.java:696)
- locked <0xc9553ae0> (a java.io.BufferedWriter)
at qstatsClient.main(qstatsClient.java:337)
"VM Thread" prio=3 tid=0x0004b800 nid=0x3 runnable
"VM Periodic Task Thread" prio=3 tid=0x000a3800 nid=0x9 waiting on condition
From the looks of this dump, it seems that I am dealing with a locked resource. Can anyone tell me how to get around this situation?
Thanks in advance for your comments.
Frustrated programmer
Re: Network communication hangs
what is 'out'? I may be out of my depth - I don't have a java.net.SocketOutputStream in my API! It seems to me like you're attempting (implicitly) a flush on a stream with a limited buffer and the other end isn't emptying the buffer (reading).
Re: Network communication hangs
Sorry, I wasn't detailed enough in showing how I had created my network socket and the out object. The following code shows this:
try {
serverSocket = new ServerSocket(port);
} catch (UnknownHostException e) {
System.err.println("Could not listen on port: " + port);
System.exit(1);
} catch (IOException e) {
System.out.println("Couldn't get I/O for the connection to denqt18.");
System.exit(0);
}
try {
System.out.println("Wait for client -test"+appServerNo+"- to accept server socket at port "+port+" --");
clientSocket = serverSocket.accept();
System.out.println("Client Socket ("+port+") Test"+appServerNo+" established --");
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
out = new PrintWriter(clientSocket.getOutputStream(),true);
Re: Network communication hangs
What is the client? Are you sure it's reading from the stream that's being written to by 'out'? Socket's typically (I think) have a fairly small buffer - you can't keep writing into it if it's not being emptied.
Re: Network communication hangs
The socket is an auto-flush socket. I also tried explicitly flushing the 'out' object, ( out.flush(); ) but it didn't change anything.
Re: Network communication hangs
I can see that, but it's a network socket - what is on the other end, emptying its limited-size buffer? You can't keep writing into that socket without something else emptying it from the other end - as far as I know, I could be wrong. If you've written the client that connects to the socket in your code here, perhaps it isn't reading?