/*
 * error on " byte[] m=args[0].getBytes();" 
 error: array index out of bound exception, help me please
 */
import java.io.*;
import java.net.*;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;


public class UDPClient {


    public static void main(String[] args) {
        System.out.println("client started");
        DatagramSocket aSocket = null;


        try {
            aSocket = new DatagramSocket();
            byte[] m = args[0].getBytes();
            InetAddress aHost = InetAddress.getByName(args[1]);
            int serverPort = 6789;
            DatagramPacket request = new DatagramPacket(m,
                    args[0].length(), aHost, serverPort);
         System.out.println("sending request = " + new String(request.getData()));

            aSocket.send(request);
            byte[] buffer = new byte[1000];
            DatagramPacket reply = new DatagramPacket(buffer,
                    buffer.length);
            aSocket.receive(reply);
            System.out.println("Reply: " + new String(reply.getData()));
        } catch (SocketException e) {
            System.out.println("Socket: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("IO: " + e.getMessage());
        } finally {
            if (aSocket != null) {
                aSocket.close();
            }
        }
    }

}

There is no error in the program, When u run the jar... U must run like this.

java UDPClient message ipaddress //------------ message can be any message u want to transfer while ipaddress is needed of destination computer

In your program args[0] point to message while arg[1] point to ip address. If u are not running through DOS, then u must have to set the running arguements in the IDE, Like in eclipse you have to go to
Run As -> Run Configuration -> Arguments -> Program Arguments and provide the necesary argurmenets with spaces.

Hope this Help ....................

I think he did not write this script himself because he coild have spoted that the
error was due to zero info in the String array that he is trying to index.

To make things simple for you,hard code your ip address and add a readin to your script
to take in messages that needs to be sent.

Start from there. Let me know if you will need that help but you must do something yourself first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.