Hi, I was trying to figure this out. I've been scratching my head all morning and I'm at a mental dead end. Can anyone help me?

Malicious code in red

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ping;

import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class PingClient
{
    private static final int PING_MESSAGES = 10;
    private static final int TOKEN_TIMESTAMP = 2;
    private static final int MAX_WAIT_TIME = 1000;
    private static final String CRLF = "\r\n";

    public static void main(String[] args) throws Exception
    {
        // If user doesn't input both port number and ip address, the program will display an error message.
        if (args.length != 2)
        {
            System.out.println("usage: java PingClient <Host> <Port>");
            //return;
        }
        
        try
        {
            if(!args[0].isEmpty())
                System.out.println("\nHost: "+args[1]+"\nIP address: "+args[0]+"\n");
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println("Host name and ip address please");
            System.exit(1);
        }
        byte[] buffer = new byte[1024];  
        //InetAddress host = InetAddress.getByName(args[0]);
        InetAddress host = InetAddress.getByName("localhost");
        
        int portNumber = Integer.parseInt(args[1]);
        
        //Create a datagram socket used for sending and recieving UDP packets
       // DatagramSocket socket = new DatagramSocket(new byte[1024],9876, 172.16.202.88, 1024);
        
        DatagramSocket socket = new DatagramSocket(buffer, buffer.length);
        
        //Set up the maximum time the socket waits for responses
        socket.setSoTimeout(MAX_WAIT_TIME);

        //Construct a ping message to be sent to the Server
        for (int sequence_num = 0; sequence_num < PING_MESSAGES; sequence_num++)
        {
                String message = generatePing(sequence_num);
                DatagramPacket ping_request =
                    new DatagramPacket(message.getBytes(), message.length(), host, portNumber);

                //Send a ping request
                socket.send(ping_request);

                //Datagram packet to hold server response
                DatagramPacket ping_response =
                    new DatagramPacket(new byte[message.length()], message.length());

                //Wait for ping response from server
                try
                {
                        socket.receive(ping_response);
                        printData(ping_response);
                }
                catch (SocketTimeoutException e)
                {
                        System.out.println("No response was received from the server");
                }
                catch (Exception e)
                {
                        //Another unknown error may have occured that can't be handled
                     e.printStackTrace(System.out);
                       return;
                }
        }
    }

    private static String generatePing(int sequence_num)
    {
        // For getting current date and time 
        SimpleDateFormat sdfNow = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String strNow = sdfNow.format(new Date(System.currentTimeMillis()));
        return "PING #" + sequence_num + " " + System.currentTimeMillis() + " ("+strNow+")";
    }

    //Print ping page to standard output stream
    private static void printData(DatagramPacket request) throws Exception
    {
        String response = new String(request.getData());
        String[] tokens = response.split(" ");
        
        //Create sent and received timestamps for RTT
        long sent_timestamp = new Long(tokens[TOKEN_TIMESTAMP]);
        long received_timestamp = System.currentTimeMillis();

        //RTT
        long rtt = received_timestamp - sent_timestamp;

        //Display results
        System.out.print(response+" Received from "+
                request.getAddress().getHostAddress() + " "+"(RTT=" + rtt + "ms)"+CRLF);
    }
}

Study the API documentation for DatagramSocket. It tells you exactly what parameters you can pass to the constructor (neither of yours has a valid parameter list). Judging by the parameters you tried to use, you are probably confusing DatagramSocket and DatagramPacket.

This article has been dead for over six months. Start a new discussion instead.