DatagramSocket Server : UDP « Network Protocol « Java






DatagramSocket Server

    
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

class WriteServer {

  public static void main(String args[]) throws Exception {
    int serverPort = 998;

    int buffer_size = 1024;

    byte buffer[] = new byte[buffer_size];

    DatagramSocket ds = new DatagramSocket(serverPort);
      int pos = 0;
      while (true) {
        int c = System.in.read();
        switch (c) {
        case -1:
          System.out.println("Server Quits.");
          return;
        case '\r':
          break;
        case '\n':
          ds.send(new DatagramPacket(buffer, pos, InetAddress.getLocalHost(), 999));
          pos = 0;
          break;
        default:
          buffer[pos++] = (byte) c;
        }
      }

  }
}

   
    
    
  








Related examples in the same category

1.Udp Echo Server
2.DatagramSocket Client
3.Send out UDP pockets
4.Receive UDP pocketsReceive UDP pockets
5.Experiment with UDP sockets
6.Using Datagrams to Get the Date
7.An echo server using UDP socketsAn echo server using UDP sockets
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.