Buffered Reader is missing data
As part of a program I am writing I am looking to do some MTU discovery, but the results aren't coming through. Any suggestions?
Code:
private static void __testMTU() {
int base = 0;
int limit = _length;
int attemptNumber = 1;
int attempt = 0;
boolean isFragmented = false;
while(base!=limit){
if(attemptNumber!=1){
attempt = (base+limit)/2;
} else {
attempt = limit;
}
String stmt = "ping ";
if(_OS.equalsIgnoreCase("win")){
stmt += "-n 1 -f -l "+attempt+" "+_target;
} else if (_OS.equalsIgnoreCase("mac") || _OS.equalsIgnoreCase("nix")){
stmt += "-c 1 -s "+attempt+" -D "+_target;
} else {
_summary += "MTU can not be tested on a Solaris system as the ping utility does not support the Do Not Fragment Flag.";
break;
}
try {
Process p=Runtime.getRuntime().exec(stmt);
p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
_summary += stmt+"\n";
String line=reader.readLine();
while(line!=null) {
_summary += line+"\n";
if(line.lastIndexOf("truncated") >= 0 || line.lastIndexOf("fragmented") >= 0 || line.lastIndexOf("too long") >= 0) {
isFragmented = true;
}
line=reader.readLine();
}
}
catch(Exception e) {}
if(attemptNumber==1 && !isFragmented) {
break;
}
if(attemptNumber==1 && isFragmented) {
_summary += "Your packet size exceeds the MTU for this connection and will be adjusted to equal the maximum MTU possible.";
}
if(isFragmented){
limit = attempt;
} else {
base = attempt;
}
attemptNumber++;
}
_length = base;
}
The script returns:
Quote:
ping -c 1 -s 64565 -D Google
PING Google (74.125.130.103): 64565 data bytes
--- Google ping statistics ---
1 packets transmitted, 0 packets received, 100.0% packet loss
but executing the command from the terminal returns:
Quote:
localhost:bin john$ ping -c 1 -s 64565 -D Google
PING Google (74.125.130.103): 64565 data bytes
ping: sendto: Message too long
--- Google ping statistics ---
1 packets transmitted, 0 packets received, 100.0% packet loss
I really need that Message too long statement. :)
Re: Buffered Reader is missing data
You're waiting for another process to complete and only then are you trying to read its output. Don't wait for its completion (its output buffer might fill up) but start reading its output right away.
kind regards,
Jos