/*
 netjtest.java - the VRML net java test client

 Copyright (C) 2002    Peter Graf

 This file is part of VRMLNETJ - The VRML net java server.
 VRMLNETJ is free software.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   For more information on the VRML net java server or Peter Graf,
   please see: http://www.mission-base.com/.

   $Log: netjtest.java,v $

------------------------------------------------------------------------------
 */
import java.io.*;
import java.net.*;

/**
 * This class sends the specified texts as a datagram to the 
 * specified port of the specified host.
 **/
public class netjtest
{
    public static final String usage = 
        "Usage: java netjtest <hostname> <port> <msg>...";

    public static void main( String args[] )
    {
        try
        { 
            // Check the number of arguments
            if( args.length < 3 ) 
            {
                throw new IllegalArgumentException( "Wrong number of args ");
            }
            
            // Parse the arguments
            String host = args[ 0 ];
            int port = Integer.parseInt( args[ 1 ] );
        
            // Figure out the message to send.  
            byte buffer[] = new byte[ 32 * 1024 ];
            int  offset = 0;

            String arg = args[ 2 ];
            byte[] barg;

            for( int i = 2; i < args.length; i++ )
            {
                arg = args[ i ];
                barg = arg.getBytes();

                if( offset + barg.length < buffer.length - 1 )
                {
                    System.arraycopy( barg, 0, buffer, offset, barg.length );
                    offset += barg.length;
                    buffer[ offset++ ] = 0;
                }
                else
                {
                    i = args.length;
                }
            }

            // Get the internet address of the specified host
            InetAddress address = InetAddress.getByName(host);
        
            // Print a log
            //
            System.out.println( "Sending " + offset + " bytes" );

            // Initialize a datagram packet with data and address
            DatagramPacket packet = new DatagramPacket( buffer,
                                                        offset,
                                                        address,
                                                        port );
        
            // Create a datagram socket, send the packet through it, close it.
            DatagramSocket dsocket = new DatagramSocket();
            dsocket.send( packet );
            dsocket.close();
        }
        catch (Exception e) {
            System.err.println(e);
            System.err.println(usage);
        }
    }
}
