UdpEchoServer.java Source code

Java tutorial

Introduction

Here is the source code for UdpEchoServer.java

Source

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UdpEchoServer {
    static final int BUFFERSIZE = 256;

    public static void main(String[] args) {
        DatagramSocket sock;
        DatagramPacket pack = new DatagramPacket(new byte[BUFFERSIZE], BUFFERSIZE);
        try {
            sock = new DatagramSocket(7);
        } catch (SocketException e) {
            System.out.println(e);
            return;
        }
        // echo back everything
        while (true) {
            try {
                sock.receive(pack);
                sock.send(pack);
            } catch (IOException ioe) {
                System.out.println(ioe);
            }
        }
    }
}