BufferedReader readLine() - how to detect end of input string

D

Dejan

Hi

I have a bug I've been trying to solve for a very long time now.
Basically my client program is using a BufferedReader.readLine to read
strings sent by the server program (I dont have the server code).

When my client reads the "</end>" tag it knows it has received the end
of the server's reply. However because there is no "\n" being sent
after the </end> tag by the server, readLine() hangs. The only way
around it I've found is using a socket timeout but that's no good
because the server can sometimes take a while to reply (processing).

It seems this is a common problem and I've found lots of posts, but no
solutions. Please help! Any ideas at this stage would be more than
welcome.
Thanks
 
G

Gordon Beaton

I have a bug I've been trying to solve for a very long time now.
Basically my client program is using a BufferedReader.readLine to
read strings sent by the server program (I dont have the server
code).

When my client reads the "</end>" tag it knows it has received the
end of the server's reply. However because there is no "\n" being
sent after the </end> tag by the server, readLine() hangs. The only
way around it I've found is using a socket timeout but that's no
good because the server can sometimes take a while to reply
(processing).

For a quick solution, subclass BufferedReader with a custom class that
overrides readLine(), so you can detect that particular sequence of
characters and return early. Your method needs to use the methods of
the superclass for reading input one *character* at a time.

Buffer the 6 most recent characters seen. Each time you see a '>'
compare the saved characters with "</end>" and return the "line" early
if they match.

This essentially adds a linebreak after every occurrence of "</end>",
so it assumes that the tag really means end in all contexts, or that
extra linebreaks aren't significant to your application.

/gordon
 
E

Eric Sosman

Dejan said:
Hi

I have a bug I've been trying to solve for a very long time now.
Basically my client program is using a BufferedReader.readLine to read
strings sent by the server program (I dont have the server code).

When my client reads the "</end>" tag it knows it has received the end
of the server's reply. However because there is no "\n" being sent
after the </end> tag by the server, readLine() hangs. The only way
around it I've found is using a socket timeout but that's no good
because the server can sometimes take a while to reply (processing).

It seems this is a common problem and I've found lots of posts, but no
solutions. Please help! Any ideas at this stage would be more than
welcome.

Off-hand, I'd say you're using the wrong input method.
readLine is for input that consists of "lines," and the
server evidently doesn't always put its data in "line" form.
Try using an input method that doesn't require "line" input
but can return the uninterpreted data bytes.
 
D

Dejan

Thanks for the replies.
I should have noted that a typical server response is a few lines
long, and on the last line contains the </end> tag with no line feed.

I'll try using BufferedReader.read() to read a single char at a time
and let you know how it goes. Presumabley read() returns -1 after
receiving all the lines, and after the </end> tag?

Dejan
 
D

Dejan

Thanks guys!
Using BufferedReader.read() worked like a charm. Since I wanted the
entire reply from the server in one go, I put the read() inside a
while(){} loop, and did an indexOf("</end>") to be the exit condition.

Gordon maybe I'm not thinking logically but how would buffering help
me, if I need the entire reply (several lines or more) in one go?
 
M

mromarkhan

Hi

I have a bug I've been trying to solve for a very long time now.
Basically my client program is using a BufferedReader.readLine to read
strings sent by the server program (I dont have the server code).

When my client reads the "</end>" tag it knows it has received the end
of the server's reply. However because there is no "\n" being sent
after the </end> tag by the server, readLine() hangs. The only way
around it I've found is using a socket timeout but that's no good
because the server can sometimes take a while to reply (processing).

It seems this is a common problem and I've found lots of posts, but no
solutions. Please help! Any ideas at this stage would be more than
welcome.
Thanks


Peace be unto you.

How about BufferedReader.read(char[] cbuf,int off,int len)

Some sample code
<code file_name="GetItAll.java">
import java.net.*;
import java.io.*;
public class GetItAll
{
public static void main(String [] args)
{
RunThread sunServer = new
RunThread(GetItAll.decimalToIP(3522786499l),
3522786499l);
sunServer.start();
}
public static String decimalToIP(long decimalNumber)
{
String binary = Long.toString(decimalNumber, 2);
String emptyBinaryString = "00000000000000000000000000000000";
int endIndex = 32 - binary.length();
String fB = emptyBinaryString.substring(0,endIndex)+binary;
Integer firstByte = Integer.valueOf(fB.substring(0,8),2);
Integer secondByte = Integer.valueOf(fB.substring(8,16),2);
Integer thirdByte = Integer.valueOf(fB.substring(16,24),2);
Integer fourthByte = Integer.valueOf(fB.substring(24,32),2);
return firstByte + "." + secondByte + "." + thirdByte + "."
+ fourthByte;
}

public static class RunThread extends Thread
{
private String address;
private long lAddress;
public RunThread(String address, long lAddress)
{
this.address = address;
this.lAddress = lAddress;
}
public void run()
{
System.out.println(address + " = " + lAddress);
try
{
Socket socket = new Socket(address, 80);
String getRobotsTxtString = "GET /robots.txt HTTP/1.1\n"+
"Accept: */*\n"+
"Accept-Language: en\n"+
"User-Agent: OmarKhanSearchRobot/0.002 (java; U; 1.4;"+
"en-us) Omar/00000002\n"+
"Host: "+address+":80\n" +
"Connection: Close\n\n";
BufferedWriter sendOutToSocket = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream(),
"US-ASCII"));
BufferedReader readInFromSocket = new BufferedReader(
new InputStreamReader( socket.getInputStream(),
"US-ASCII"));
sendOutToSocket.write(getRobotsTxtString,0,
getRobotsTxtString.length());
sendOutToSocket.flush();
StringBuffer buffer = new StringBuffer();
while(true)
{
char[] cbuf = new char[1024];
int status = readInFromSocket.read(cbuf,0,1024);
if(status == -1)
break;
buffer.append(cbuf);
}
System.out.println(buffer);
socket.close();
}
catch(IOException e)
{
}
}
}
}
</code>
<output>
HTTP/1.1 200 OK
Server: SunONE WebServer 6.0
Date: Thu, 12 Aug 2004 03:06:12 GMT
Content-length: 2063
Content-type: text/plain
P3p: policyref="http://www.sun.com/p3p/Sun_P3P_Policy.xml", CP="CAO
DSP COR CUR ADMa DEVa TAIa PSAa PSDa CONi TELi OUR SAMi PUBi IND PHY
ONL PUR COM NAV INT DEM CNT STA POL PRE GOV"
Set-Cookie: SUN_ID=24.156.146.33:75131092279973; EXPIRES=Wednesday,
31-Dec-2025 23:59:59 GMT; DOMAIN=.sun.com; PATH=/
Etag: "9f647568-1595-0-80f"
Last-modified: Fri, 30 Jul 2004 23:57:28 GMT
Accept-ranges: bytes
Connection: close

# /robots.txt for www.sun.com

#--------------------------------------------------------------------------
# Mon Feb 2 11:59:27 PST 1998, Fred Elliott
# A NOTE TO THOSE WHO'D BOTHER TO LOOK AT THIS FILE:
[snip][snip]
</output>


Disclaimer:By the way, a search bot may be considered port scanning by
some isp's.
Because some firewalls may treat it as a Nachi/CodeRed/Nimda attack,
and send a warning to your isp through services like
http://www.mynetwatchman.com/.

Here is a sample letter from an isp
-----------------------------------
Please be advised that we have received numerous complaints regarding
unauthorized scans/probes originating from your IP address, contrary
to
the terms & conditions outlined in the Rogers End User Agreement. In
an effort to preserve the quality of service to all customers, and to
maintain a good standing presence with fellow Internet entities, we
are
contacting you to ensure that this matter is resolved immediately. A
portion of the
complaint(s) are included below this message.

'Code Red' or 'Nimda Virus' - The attached scans show that your
computer was scanning for Port 80, therefore your computer may be
infected
with either the 'Code Red' or 'Nimda Virus' . Your computer may have
become infected by either of these viruses in a number of ways (such
as
opening an Email attachment or downloading a program from the Internet
without virus scanning it first). Once a computer becomes infected
with
the Code Red or Nimda virus, the self-propagating virus continues to
spread by exploiting other computers running web services.


http://www.google.com/ - Obtain search results and post from
newsgroups
http://www.rogers.com/ - Obtain access to Internet
http://www.ultinet.ca/ - Obtain custom made computer
http://www.microsoft.com/ - Obtain Windows ME;think of it as Windows
98+
http://www.sun.com/ - Obtain Java Software Development Kit
http://www.mozilla.org/ - Obtain Firefox, has tabs and bookmark
toolbar
http://www.humberc.on.ca/ - Obtain Computer Programmer Analyst title
http://www.usc.edu/dept/MSA/quran/ - Obtain some translations of the
Quran in English
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top