- A Datagram Server Application

This section provides a tutorial example on how to write a network application,, 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:

 - Copyright (c) 2014,, All Rights Reserved.
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);
         while (true) {
            // 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;
      } catch (IOException e) {
   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 = "
         System.out.println("   Remote port = "
      } else {
         System.out.println("   Not connected to a remote system.");
      System.out.println("   Local socket address = "
      System.out.println("   Local address = "
      System.out.println("   Local port = "
   private static void printDatagramPacketInfo(DatagramPacket d) {
      System.out.println("Datagram Packet Info:");
      System.out.println("   Remote socket address = "
      System.out.println("   Remote address = "
      System.out.println("   Remote port = "
      System.out.println("   Data length = "

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:

Datagram Socket Info:
   Not connected to a remote system.
   Local socket address =
   Local address =
   Local port = 7777

The server is ready to receive packets at port 7777.

Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 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

 Socket Network Communication

Datagram Network Communication

 What Is a Datagram?

 Establishing a Datagram Communication Link - A Datagram Server Application - 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 - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 PDF Printing Version