An echo server using UDP sockets : UDP « Network Protocol « Java






An echo server using UDP sockets

An echo server using UDP sockets
   
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);
      }
    }
  }
}


           
         
    
    
  








Related examples in the same category

1.Udp Echo Server
2.DatagramSocket Server
3.DatagramSocket Client
4.Send out UDP pockets
5.Receive UDP pocketsReceive UDP pockets
6.Experiment with UDP sockets
7.Using Datagrams to Get the Date
8.Connect to a daytime server using the UDP protocol
9.Handles TCP and UDP connections and provides exception handling and error logging
10.UDP OutputStream
11.UDP InputStream
12.Performs broadcast and multicast peer detection.