// UDPSend2.java
// This example is from the book _Java in a Nutshell_ by David Flanagan.
// Written by David Flanagan.  Copyright (c) 1996 O'Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or implied.
// Modified by tsaiwn@csie.nctu.edu.tw to solve International problem

import java.io.*;
import java.net.*;

 // This class sends the specified text as a datagram to port 6010 of the 
// specified host.  Note: use quote "if the message contains special char "
 //  Note:  若訊息(message)有空白或特殊字符請用雙引號"夾住" 

public class UDPSend2 { 
    static final int port = 6010;
    public static void main(String args[]) throws Exception {
        if (args.length != 2) {
            System.out.println("Usage: java UDPSend host message");
            System.exit(0);
        }
	
        // Get the internet address of the specified host
        InetAddress address = InetAddress.getByName(args[0]);
	
        // Convert the message to an array of bytes
        int msglen = args[1].length();   // character length, not byte Len
        byte[] message = new byte[msglen+msglen];    // double by tsaiwn
        //args[1].getBytes(0, msglen, message, 0);
        message = args[1].getBytes();  //use Default charset, by tsaiwn@csie
	
        // Initilize the packet with data and address
        DatagramPacket packet = new DatagramPacket(message,
                       message.length, address, port);   //good form, tsaiwn
        // Create a socket, and send the packet through it.
        DatagramSocket socket = new DatagramSocket();
        socket.send(packet);   // send it out anyway
    } // main
}
