Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Network communication hangs

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Network communication hangs

    I am writing a program where I am collecting data via a Runtime function as in:

    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

       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


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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).

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);
    Last edited by droyhull; August 6th, 2011 at 11:06 AM. Reason: Add further explanation

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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.

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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?

Similar Threads

  1. server/client communication problem
    By perl0101 in forum Java Networking
    Replies: 8
    Last Post: May 24th, 2011, 02:58 PM
  2. Thread Hangs at split method.
    By kailasvilaskore in forum Threads
    Replies: 1
    Last Post: November 26th, 2010, 12:13 PM
  3. client server communication
    By Brt93yoda in forum Java Theory & Questions
    Replies: 4
    Last Post: September 2nd, 2010, 05:49 PM
  4. communication protocol
    By isaac in forum Java Networking
    Replies: 1
    Last Post: February 5th, 2010, 08:20 AM
  5. servlet applet communication
    By prashanthi_asn in forum Java Servlet
    Replies: 0
    Last Post: May 21st, 2009, 01:50 AM