package mil.navy.nps.testing; import java.net.*; import java.util.*; /** DatagramReader: Datagram socket reader Some test programming to figure out what's going on with datagram sockets. This just listens on a port for datagrams, accepting them and throwing them away. */ import java.io.*; import mil.navy.nps.dis.*; public class DatagramReader { public static void main(String args[]) throws IOException { DatagramPacket aPacket; // One packet read from the socket DatagramSocket theSocket; // Socket we read from byte packetBuff[] = new byte[1500]; // Buffer for datagram packet String infoString; int packetReceivedCount = 0; int idx = 0; long time1, time2, timeDifference; String sockNo = new String("8004"); boolean firstPacket = false; EntityStatePdu aPdu; time1 = 0; for(idx = 0; idx < args.length; idx++) { if((args[idx].equals("-h")) || (args[idx].equals("-?") ) ) { System.out.println("Command line options: "); System.out.println(" -sockNo Socket to listen to on this machine"); return; } if(args[idx].equals("-sockNo")) { sockNo = args[idx+1]; System.out.println("got command line arg of -sockNo with value of " + sockNo); } } System.out.println("starting up reader process..."); theSocket = new DatagramSocket(Integer.valueOf(sockNo).intValue()); aPacket = new DatagramPacket(packetBuff, 1500); while(true) { theSocket.receive(aPacket); if(firstPacket == false) time1 = System.currentTimeMillis(); firstPacket = true; //aPdu = (EntityStatePdu)ProtocolDataUnit.datagramToPdu(aPacket); packetReceivedCount++; if((packetReceivedCount) % 100 == 0) { System.out.println("Got " + packetReceivedCount + " packets"); if(packetReceivedCount >= 5000) { time2 = System.currentTimeMillis(); timeDifference = time2 - time1; System.out.println("Time required to receive" + packetReceivedCount + " packets = " + timeDifference); System.out.println("packet length of " + aPacket.getLength()); //aPdu.printValues(5); break; } } // end of if mod 100 } // end of while } // end of main } // end of class