/* * DatagramDemo.java * * This class creates a datagram socket, listens for a message from another * system on it, and then prints the message to System.out * * Copyright (c) 2000 - Russell C. Bjork * */ import java.net.*;import javax.swing.JOptionPane;public class DatagramDemo{	public static void main(String [] args) throws Exception	{		// Create a datagram socket to receive data.  The system will choose		// an ephemeral port				DatagramSocket socket = new DatagramSocket();				// Create a datagram packet to store the received data				byte [] rawData = new byte [MAX_MESSAGE_SIZE];		DatagramPacket packet = new DatagramPacket(rawData, MAX_MESSAGE_SIZE);				// Receive the data				JOptionPane.showMessageDialog(null, "Ready to receive a message at " +			InetAddress.getLocalHost().getHostAddress() + 			" port " + socket.getLocalPort());		socket.receive(packet);				// Write out the sender and information received				InetAddress sender = packet.getAddress();		JOptionPane.showMessageDialog(null, "Received message from: " + 			sender.getHostName() +			" [" + sender.getHostAddress() + "]\n" +			new String(rawData, 0, 0, packet.getLength()));	}	private static final int MAX_MESSAGE_SIZE = 100;}