Results 1 to 10 of 10
  1. #1
    hahncj55408 is offline Member
    Join Date
    Aug 2011
    Posts
    7
    Rep Power
    0

    Default Issue with blocking on an Socket inboundConenction()

    Hello,
    I was trying to create a program that worked similar to FTP, as an exercise.
    The challenge that I'm having is with the interface, in response to commands I send, the server will send X lines of text as a response.
    This would work swell if I only sent a single line back, but I wanted to send a multi-line help response as well as the multi-line directory.

    I'm trying to recieve the text in a while loop:

    Java Code:
    		BufferedReader in = new BufferedReader(new    InputStreamReader(outboundConnection.getInputStream()));
    
    		PrintWriter out = new PrintWriter (outboundConnection.getOutputStream());
    		String messageToSend;
    		String messageRecieved;
    
    		messageRecieved = in.readLine();
    			while (!messageRecieved.equals("null")){
    				System.out.println(messageRecieved);
    				messageRecieved = in.readLine();
    			
    			}
    The solution I have works, as long as the server sends a blank line at the end of its transmission. I've tried a couple other solutions but I always wind up with the program hanging, waiting for another line.
    My question is, besides having a delimiter (in my case, the blank line), is there a better way to make this work? Is there a network reader class that will check to see if any text has been received, and if it hasn't we can move on?

    Thanks!
    Chris

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    (!messageRecieved.equals("null")){
    Does the readLine method return a String containing "null" or does it return a null value?
    If the later, you would compare the value of the variable to null vs seeing if the String contained the value "null".
    Hint: here is where you use the == operator.

  3. #3
    hahncj55408 is offline Member
    Join Date
    Aug 2011
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the reply, I thought I had saved the project in a position where it worked, but I guess I hadn't.
    This is the code that works. It only works if the server includes an extra NewLine at the end of the message (so an empty line to delimit the end of the message).

    Java Code:
    		while(true) {
    			messageRecieved = in.readLine();
    			while (!messageRecieved.isEmpty()){
    				//System.out.println("reading message");
    				System.out.println(messageRecieved);
    				messageRecieved = in.readLine();
    				
    			}
    			System.out.print(">");
    			messageToSend = TextIO.getlnString();
    			out.print(messageToSend + "\n");
    			out.flush();
    			System.out.println("message sent");
    			if (messageToSend.toLowerCase().startsWith("quit"))
    				return;
    			
    		}
    So this code works for my purposes. I'm trying to advance my Java technique though. I would rather the server just send the message and the client prints what it recieves, and if there has been nothing recieved on the socket it'll move on to the next step. Here, if there is nothing on the socket the .readLine() method will wait for something to be sent.
    See what I mean?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    What if readLine returns null?

  5. #5
    hahncj55408 is offline Member
    Join Date
    Aug 2011
    Posts
    7
    Rep Power
    0

    Default

    I tried that,
    if (messageRecieved != null)...

    It prints the initial greeting sent by the server (both lines), but then the program locks. I believe again at the readLine() method.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Test for null first and then test for an empty String second.
    the program locks
    It would be waiting on the server to send some more data. How do you detect the end of a message?
    an extra NewLine at the end of the message
    Then your code must recognize the empty line as the end of the message.

  7. #7
    hahncj55408 is offline Member
    Join Date
    Aug 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Then your code must recognize the empty line as the end of the message.
    It does this and this works. My question is really about form and protocol. In this protocol, the server signals it has finished its transmission by sending an empty line. In the opinions of the Java masters on this site, is this the preferred protocol to use?
    This would be the same to me as sending a single line that says [STOP].
    If you were writing this client, you wanted to read the message from the server but did not know in advance how many lines or characters the message contained, how would you program it?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    I guess it depends on the protocol
    The HTTP protocol puts an empty line between the header and the body of the text.
    The Content-length field gives the length in bytes of the body.

    Some how you need to know the full length of the message. Also since the connection could be closed you should look for and handle readLine returning a null.

  9. #9
    hahncj55408 is offline Member
    Join Date
    Aug 2011
    Posts
    7
    Rep Power
    0

    Default

    Very good, thanks for the info. I hadn't thought about the null case as well, so I will be sure to do that in the future.

    Chris

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    You can add the null test as the first condition:
    if(msg == null || msg.isEmtpy()) ...

Similar Threads

  1. Replies: 0
    Last Post: 06-14-2011, 07:51 PM
  2. non-blocking SSL socket server
    By e_scape in forum Networking
    Replies: 0
    Last Post: 04-12-2011, 06:18 PM
  3. best way to signal a thread that's blocking on a socket receive
    By danieru in forum Threads and Synchronization
    Replies: 5
    Last Post: 09-08-2009, 05:47 PM
  4. Creating Socket Issue
    By castiel in forum Networking
    Replies: 2
    Last Post: 08-25-2009, 02:34 PM
  5. writting extended ascii chars on socket........or Endianness Issue......??
    By sachinj13 in forum Threads and Synchronization
    Replies: 8
    Last Post: 09-23-2008, 03:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •