/** * UdpPerturb.java * * Created: Sat Jul 8 15:19:57 2000 * * @author David Green * @version */ import java.io.*; import java.net.*; public class UdpPerturb { public static void main(String[] args) throws IOException { int remotePort; InetAddress remoteHost; byte [] sendBuffer; byte [] receiveBuffer = new byte[1400]; String userText; PrintWriter console = new PrintWriter( System.out ); BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); if ( args.length != 2 ) { console.println( "Usage: java UdpPerturb host port"); console.flush(); return ; } try { remotePort = Integer.parseInt( args[1], 10 ); remoteHost = InetAddress.getByName( args[0] ); } catch ( IOException ex ) { console.println( "Bad parameters: " + ex ); return; } DatagramSocket s = new DatagramSocket( ); DatagramPacket receiveP = new DatagramPacket( receiveBuffer, receiveBuffer.length); while ( true ) { console.print( "Send: " ); console.flush(); userText = keyboard.readLine(); sendBuffer = userText.getBytes(); DatagramPacket sendP = new DatagramPacket( sendBuffer, sendBuffer.length, remoteHost, remotePort ); // note - you must reset the buffer length since the buffer // length is both 'how much space there is' pre-recieve // and how many were received (post-receive) receiveP.setLength( receiveBuffer.length ); s.send( sendP ); console.println("Sent! Now waiting on response..."); s.receive( receiveP ); console.println("Received: " + new String( receiveP.getData(), 0, receiveP.getLength() ) ); console.flush(); } } } // UdpPerturb