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 3 of 3

Thread: How to use PrintSteam and BufferedReader Simultaneously

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to use PrintSteam and BufferedReader Simultaneously

    Hello,

    I am working with an electronic device that receives commands and sends ASCII data to the client using the socket functionality in Java. I have no problems using PrintStream sending commands or using BufferedReader to receive data from the electronic device separately. The problem I am currently having is receiving data from the electronic device immeditately after I send it a command string. For example there is a command string that I can send to the device and by doing so the device will return the settings in ASCII data.

    Here is my code:

    try 
              {   
     
                //Create a connection to server
                Socket s = new Socket(ipAddress, portNumber);
                //Create input and output streams to socket
                PrintStream out = new PrintStream(s.getOutputStream());
                //Create input streams to socket
                BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
     
                //send the command string to the barcode reader 
                out.print("sRN LocationName");  
     
                //receive data from barcode reader that the command string initiated
                String line = in.readLine();
                System.out.println("Text received: " + line);
     
                //close the stream
                out.close();
     
                //close the stream
                in.close();
     
                //close the socket
                s.close();
     
            }catch (SocketException e )
            {
                //for debugging purposes
                System.err.println ("Socket error : " + e);
     
            }catch (UnknownHostException e )
            {
                //for debugging purposes
                System.err.println ("Invalid host!");
     
            }catch (IOException e )
            {
                //for debugging purposes
                System.err.println ("I/O error : " + e);
     
            }

    Everytime I try it I get no return data through BufferedReader. I know it works because I have tested in Hyperterminal and it works everytime.

    Any help is appreciated.

    Thanks in advanced


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to use PrintSteam and BufferedReader Simultaneously

    I'm not completely positive if this will fix your problem, but I know that sometimes you have to manually flush the stream. You could give that a go and see if it works.

    You can check to see if your program is actually sending something out the socket by using a profiling program (I recommend WireShark, it's a very nice open-source network monitor). Conversely, you can also check to see if you're receiving anything back.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How to use PrintSteam and BufferedReader Simultaneously

    Each time you send the message from your device is submitted successfully, but you try to read it as fast as it's not still flushed. Use flush() to clear your output memory buffer and force it to write over the destination.
    Remember, flush() will throw IO exception, handle it and i don't think if outputstream or bufferedstream will allow flush() as documentation says, they both don't work.
    Anyways, give it a try, may be it will work.

Similar Threads

  1. Send and Receive data from socket simultaneously
    By bigmac025 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 25th, 2011, 05:18 PM
  2. Replies: 1
    Last Post: December 6th, 2010, 10:09 AM
  3. BufferedReader
    By reg4ltip in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 20th, 2010, 12:15 PM
  4. Problem in BufferedReader
    By shamed in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 1st, 2009, 04:30 PM
  5. Scanner vs BufferedReader?
    By Bill_H in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: October 27th, 2009, 09:44 AM