* 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()));

            byte[] buffer = new byte[1000];
            DatagramPacket reply = new DatagramPacket(buffer,
            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) {

4 Years
Discussion Span
Last Post by richieking

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.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.