FileDocCategorySizeDatePackage
UDPEchoServer.javaAPI DocExample1075Sat Sep 09 20:51:42 BST 2000None

UDPEchoServer.java

import java.net.*;
import java.io.*;

public class UDPEchoServer extends UDPServer {

  public final static int DEFAULT_PORT = 7;

  public UDPEchoServer() throws SocketException {
    this(DEFAULT_PORT); 
  }

  public UDPEchoServer(int port) throws SocketException {
    super(port); 
    ds.setReceiveBufferSize(32768);
  }

  public void respond(DatagramPacket incoming) {

    try {
      DatagramPacket outgoing = new DatagramPacket(incoming.getData(), 
       incoming.getLength(), incoming.getAddress(), incoming.getPort());
      ds.send(outgoing);
      System.out.println(incoming.getLength());
    }
    catch (IOException e) {
      System.err.println(e);
    }
    
  }

  public static void main(String[] args) {
 
   try {
     if (args.length > 0) {
       int port = Integer.parseInt(args[0]);
       UDPEchoServer server = new UDPEchoServer(port);
       server.start();       
     }
     else {
       UDPEchoServer server = new UDPEchoServer();
       server.start();
     }
   }
   catch (SocketException e) {
     System.err.println(e);
   }
 
  }

}