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

Thread: Buffered Reader is missing some data

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Buffered Reader is missing some 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?

        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:
    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:
    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.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Buffered Reader is missing some data

    Quote Originally Posted by cjtemple View Post
    ...
    I really need that Message too long statement...
    Here's what works with Linux (Tested with Centos 5.8, kernel 2.6.18.something, and Java 1.6.0_22):

    Capture the stderr output from the process by opening a reader on the error stream.

    To test this:
                    // For Debugging purposes, show the command
                    System.out.printf("Executing \"%s\"\n", stmt);
     
                    Process p=Runtime.getRuntime().exec(stmt);
     
                    BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
                    BufferedReader stderr=new BufferedReader(new InputStreamReader(p.getErrorStream()));
     
                    p.waitFor();
     
                   String errLine = stderr.readLine();
                    //
                    // For Debugging purposes, just print the line
                    //
                    if (errLine != null) {
                        System.out.printf("From stderr: %s\n", errLine);
                        // Set a flag or something???
                    }
                    String line=reader.readLine();
                    while (line != null) {
    .
    . // Whatever...
    .

    For my system, after modifying the "ping" command to be consistent with my version, when trying to ping with a packet size of 100000 bytes, I get


    Executing "ping -c 1 -s 100000 google.com"
    From stderr: Error: packet size 100000 is too large. Maximum is 65507


    And there is no output from stdout (The "line" String is null.)

    Also, note that System.getProperty("os.name") yields the String "Linux" on my system, so I also had to modify your OS identification logic to get it to run.

    Bottom line for all OS-dependent issues/solutions: IWFMYMMV (It Works For Me; Your Mileage May Vary.)


    Cheers!

    Z

Similar Threads

  1. help me with my buffered reader please! whats wrong?
    By jeremanalaotao in forum Loops & Control Statements
    Replies: 10
    Last Post: September 6th, 2011, 07:09 PM
  2. Double buffered Image is flickering
    By jay-wo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 29th, 2011, 11:21 PM
  3. Buffered Reader to J Option
    By jk_0821 in forum Collections and Generics
    Replies: 13
    Last Post: July 19th, 2010, 03:14 PM
  4. Buffered Reader is not reading my file properly... HELP!
    By mannyT in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: November 8th, 2009, 07:14 PM
  5. Buffered Reader to display file content after calcuation
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 19
    Last Post: May 14th, 2008, 03:23 AM