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