JDK (Java Development Kit) Tutorials
Dr. Herong Yang, Version 5.00

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) 2002 by Dr. Herong Yang
 */
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:

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.

Last update: 2006.

Sections in This Chapter

What Is a Datagram?

Establishing a Datagram Communication Link

DatagramServer.java - A Datagram Server Application

DatagraClient.java - A Datagram Client Application

Dr. Herong Yang, updated in 2008
DatagramServer.java - A Datagram Server Application