Client code

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

/**
 * This example illustrates the basic method calls for connectionless
 * datagram socket.
 * @author M. L. Liu
 */
public class Example1Sender {

// An application which sends a message using connectionless
// datagram socket.
// Three command line arguments are expected, in order: 
//    <domain name or IP address of the receiver>
//    <port number of the receiver's socket>
//    <message, a string, to send>

   public static void main(String[] args) {
      if (args.length != 3)
         System.out.println
            ("This program requires three command line arguments");
      else {
         try {
              InetAddress receiverHost = InetAddress.getByName(args[0]);
              int receiverPort = Integer.parseInt(args[1]);
            String message = args[2];

            // instantiates a datagram socket for sending the data
          DatagramSocket    mySocket = new DatagramSocket();           
            byte[ ] buffer = message.getBytes( );                                     
            DatagramPacket datagram = 
               new DatagramPacket(buffer, buffer.length, 
                                  receiverHost, receiverPort);
            mySocket.send(datagram);
            mySocket.close( );
         } // end try
     catch (Exception ex) {
       ex.printStackTrace( );
     }
      } // end else
   } // end main
} // end class

Server code

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

/**
 * This example illustrates the basic method calls for connectionless
 * datagram socket.
 * @author M. L. Liu
 */
public class Example1Receiver {

// An application which receives a message using connectionless
// datagram socket.
// A command line argument is expected, in order: 
//    <port number of the receiver's socket>
// Note: the same port number should be specified in the
// command-line arguments for the sender.

   public static void main(String[] args) {
      if (args.length != 1)
         System.out.println
            ("This program requires a command line argument.");
      else {
            int port = Integer.parseInt(args[0]);
         final int MAX_LEN = 10; 
            // This is the assumed maximum byte length of the 
            //      datagram to be received.
            try {
          DatagramSocket    mySocket = new DatagramSocket(port);  
               // instantiates a datagram socket for receiving the data
            byte[ ] buffer = new byte[MAX_LEN];                                     
            DatagramPacket datagram = 
               new DatagramPacket(buffer, MAX_LEN);
            mySocket.receive(datagram);
            String message = new String(buffer);
            System.out.println(message);
            mySocket.close( );
         } // end try
     catch (Exception ex) {
        ex.printStackTrace( );
     }
      } // end else
   } // end main
} // end class

What should I enter in the command prompt to make it work?

Can I choose my own IP and any non-dedicated port number such as 88888 and 88889?

 InetAddress receiverHost = InetAddress.getByName(args[0]);
              int receiverPort = Integer.parseInt(args[1]);
            String message = args[2];

you need to pass as arguments:

  • the InetAddress
  • the receiverPort
  • the message

in that order.

since we don't know your configuration it'll be pretty hard to be more specific.

To expand on the previous answer look at this line of code... Apologies if you're already familiar with this but given the question I got the impression you might not have had this explained to you.

public static void main(String[] args)

This method called main is the method/function that runs when you execute a program from the command line. This is generally speaking always the case. When reading someone else's command line program code, look for this line as it's the place the code starts to execute.

Anything you type into the command line as a paramater to the program gets translated directly into a parameter for this method.

This method called main takes as its parameter an array of Strings called args so any words you type into the command line become elements in the array called args when it runs.

to illustrate;
java myProgram first second hello 1024.121

Thsi will start the program and args will have the following values in it:
args[0] == "first"
args[1] == "second"
args[2] == "hello"
args[3] == "1024.121"

From there it is a simple case of looking through the code for places where 'args' is referenced, ie what method wants args[0], and working out what you need to pass in.

That's how stultuske was able to identify the required parameters for the Sender. It looks like the Receiver has only 1 parameter.

Worth noting that args does not have to be called args. you could quite easily have the main method look like this:

public static void main(String[] shiverMeTimbers)

This would however be bad practise and is simply put, a little bit silly so you're unlikely to see it a lot.

Can I choose my own IP and any non-dedicated port number such as 88888 and 88889?

You can't have 88888 as your port number. Quoting wikipedia:

A port number is a 16-bit unsigned integer, thus ranging from 1 to 65535 (for TCP, port number 0 is reserved and can't be used. For UDP the source port is optional and a value of zero means no port)

the InetAddress
the receiverPort
the message

I was asking if I could use my own ip for the server and client and then choose an arbitrary port number like 65534 (server) and 65535 (client)

Hmm, yeah. I entered the same port number for both and the ip address I got from google as parameters and it doesn't work.

Looks to me as if it should work if you start the Receiver first like this:

java -jar Example1Receiver.jar 4444

Then you should start the client (use the IP of the server instead of 192.168.0.2):

java -jar Example1Sender.jar 192.168.0.2 4444 "Hello World"

(Could be you have to type "192.168.0.2").

Martin

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.