DatagramServer.java - A Datagram Server Application

This section provides a tutorial example on how to write a network application, DatagramServer.java, that creates a datagram server socket and listens for incoming packets.

The following program, called DatagramServer, is a simple datagram socket application, which acts a server listening on given port number for datagram packets to arrive:

/* DatagramServer.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.io.*;
import java.net.*;
public class DatagramServer {
   public static void main(String[] args) {
      byte[] buf = new byte[1024];
      DatagramPacket dp = new DatagramPacket(buf, buf.length);
      try {
         // binding to the default address and port 7777
         DatagramSocket ds = new DatagramSocket(7777);
         printDatagramSocketInfo(ds);
         while (true) {
            ds.receive(dp);
            printDatagramSocketInfo(ds);
            printDatagramPacketInfo(dp);
            // no need to call buf = dp.getData();
            // reversing the characters, assuming one-type characters
            int n = dp.getLength();
            for (int i=0; i<n/2; i++) {
               byte t = buf[i];
               buf[i] = buf[n-1-i];
               buf[n-1-i] = t;
            }
            ds.send(dp);
         }
      } 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());
   }
   private static void printDatagramPacketInfo(DatagramPacket d) {
      System.out.println("Datagram Packet Info:");
      System.out.println("   Remote socket address = "
         +d.getSocketAddress().toString());
      System.out.println("   Remote address = "
         +d.getAddress().toString());
      System.out.println("   Remote port = "
         +d.getPort());
      System.out.println("   Data length = "
         +d.getLength());
   }
}

Whenever DatagramServer receives a datagram packet, it takes the data, reverses the data, and sends the data back to where it comes from.

Run DatagramServer in a command window, you will see the following output:

herong> java DatagramServer.java

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

The server is ready to receive packets at port 7777.

Table of Contents

 About This JDK Tutorial Book

 JDK (Java Development Kit)

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Java Logging

 Socket Network Communication

Datagram Network Communication

 What Is a Datagram?

 Establishing a Datagram Communication Link

DatagramServer.java - A Datagram Server Application

 DatagraClient.java - A Datagram Client Application

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials

 References

 Full Version in PDF/EPUB