1 package org.jgroups.tests; 2 3 import java.io.IOException ; 4 import java.net.DatagramPacket ; 5 import java.net.DatagramSocket ; 6 import java.net.InetAddress ; 7 8 15 public class DetermineFragSize { 16 17 18 static int senseMaxFragSize() { 19 int upper=4096; 20 int lower=0; 21 int highest_failed=-1; 22 DatagramSocket sock; 23 byte[] buf; 24 DatagramPacket packet; 25 InetAddress local_addr; 26 final int num_iterations=15; 27 28 try { 29 sock=new DatagramSocket (); 30 local_addr=InetAddress.getLocalHost(); 31 } 32 catch(Exception ex) { 33 System.err.println("failed creating DatagramSocket: " + ex); 34 return lower; 35 } 36 37 for(int i=0; i < num_iterations && lower < upper; i++) { try { 39 buf=new byte[upper]; 40 packet=new DatagramPacket (buf, buf.length, local_addr, 9); 42 sock.send(packet); 43 lower=Math.max(lower, upper); 44 System.out.println("-- trying " + lower + " [OK]"); 45 upper=upper * 2; 46 if(highest_failed > -1) 47 upper=Math.min(highest_failed, upper); 48 } 49 catch(IOException io_ex) { 50 if(highest_failed > -1) 51 highest_failed=Math.min(highest_failed, upper); else 53 highest_failed=upper; 54 upper=(upper + lower) / 2; 55 } 56 catch(Throwable ex) { 57 ex.printStackTrace(); 58 break; 59 } 60 } 61 return lower; 62 } 63 64 65 public static void main(String [] args) { 66 DatagramSocket sock; 67 DatagramPacket packet; 68 int size=0, frag_size=0; 69 byte[] buf; 70 71 try { 72 size=senseMaxFragSize(); 73 System.out.println("-- fine tuning (starting at " + size + "):"); 74 sock=new DatagramSocket (); 75 for(; ;) { 76 buf=new byte[size]; 77 packet=new DatagramPacket (buf, buf.length, InetAddress.getLocalHost(), 9); 78 sock.send(packet); 79 frag_size=size; 82 size++; 83 } 84 } 85 catch(Throwable t) { 86 } 89 System.out.println("\n***** fragmentation size on your system is " + frag_size + " bytes *******\n"); 90 } 91 } 92 | Popular Tags |