JDK Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.32, 2006

Datagram Communication

Part:   1  2  3  

JDK Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Internationalization

Character Set and Encoding

Socket Communication

Document Object Model (DOM)

XSD Validation in Java

XSL - Transformer in Java

JCA - Private and Public Key Pairs

JCE - Secret Key

SSL (Secure Socket Layer)

SSL - Client Authentication

... Table of Contents

(Continued from previous part...)

/**
 * DatagramClient.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.io.*;
import java.net.*;
public class DatagramClient {
   public static void main(String[] args) {
      BufferedReader in = new BufferedReader(new InputStreamReader(
         System.in));
      PrintStream out = System.out;   	
      byte[] buf = new byte[1024];
      try {
         DatagramPacket dp = new DatagramPacket(buf, buf.length);
         dp.setAddress(InetAddress.getByName("localhost"));
         dp.setPort(7777);
         // binding to the default address and any free port
         DatagramSocket ds = new DatagramSocket();
         printDatagramSocketInfo(ds);
         String m = null;
         while (true) {
            // sending the text line from console to the remote system
            m = in.readLine();
            byte[] b = m.getBytes();
            dp.setData(b);
            dp.setLength(b.length);
            ds.send(dp);
            // sending the text line from the remote system to console
            dp.setData(buf);
            ds.receive(dp);
            m = new String(buf, 0, dp.getLength());            
            out.println(m);
            if (m.equals(".")) break;
         }
      } catch (IOException e) {
         System.err.println(e.toString());
      }
   }
   private static void printDatagramSocketInfo(DatagramSocket s) {
      System.out.println("Datagram Socket Info:");
      if (s.isConnected()) {
         System.out.println("   Connected to a remote system.");
         System.out.println("   Remote address = "
            +s.getInetAddress().toString());
         System.out.println("   Remote port = "
            +s.getPort());
      } else {
         System.out.println("   Not connected to a remote system.");
      }
      System.out.println("   Local socket address = "
         +s.getLocalSocketAddress().toString());
      System.out.println("   Local address = "
         +s.getLocalAddress().toString());
      System.out.println("   Local port = "
         +s.getLocalPort());
   }
}

One DatagramPacket object is used for both sending data and receiving data. Two byte arrays are used in the program, buf contains the data received, b contains the data to be send out.

With DatagramServer running in a command window, run DatagramClient in another command window, and type in

Fish, I love you and respect you very much.
But I will kill you dead before this day ends.
.

you will see:

Datagram Socket Info:
   Not connected to a remote system.
   Local socket address = 0.0.0.0/0.0.0.0:1035
   Local address = 0.0.0.0/0.0.0.0
   Local port = 1035
Fish, I love you and respect you very much.
.hcum yrev uoy tcepser dna uoy evol I ,hsiF
But I will kill you dead before this day ends.
.sdne yad siht erofeb daed uoy llik lliw I tuB
.

Note that the datagram socket constructor automatically picked up a free local port, 1035.

In the DatagramServer window, you will see the following output:

Datagram Socket Info:
   Not connected to a remote system.
   Local socket address = 0.0.0.0/0.0.0.0:7777
   Local address = 0.0.0.0/0.0.0.0
   Local port = 7777
Datagram Packet Info:
   Remote socket address = /127.0.0.1:1035
   Remote address = /127.0.0.1
   Remote port = 1035
   Data length = 43
Datagram Socket Info:
   Not connected to a remote system.
   Local socket address = 0.0.0.0/0.0.0.0:7777
   Local address = 0.0.0.0/0.0.0.0
   Local port = 7777
Datagram Packet Info:
   Remote socket address = /127.0.0.1:1035
   Remote address = /127.0.0.1
   Remote port = 1035
   Data length = 46
Datagram Socket Info:
   Not connected to a remote system.
   Local socket address = 0.0.0.0/0.0.0.0:7777
   Local address = 0.0.0.0/0.0.0.0
   Local port = 7777
Datagram Packet Info:
   Remote socket address = /127.0.0.1:1035
   Remote address = /127.0.0.1
   Remote port = 1035
   Data length = 1

As you can see, DatagramServer and DatagramClient worked perfectly. You can even run multiple instances of DatagramClient, and DatagramServer will handle them correctly.

Part:   1  2  3  

Dr. Herong Yang, updated in 2006
JDK Tutorials - Herong's Tutorial Notes - Datagram Communication