//: udpServer0.java // SYNOPSIS: udpServer0 // DESCRIPTION: This program creates a datagram socket in the inet // domain, binds it to port 12345 and receives any message // arrived to the socket and prints it out //////////////////////////////////////////////////////////////////////// import java.net.*; import java.io.*; import java.util.*; public class udpServer0 { // Server Port static final int INPORT = 12345; private byte[] buf = new byte[1000]; // Create a datagram packet private DatagramPacket dp = new DatagramPacket(buf, buf.length); // Create a Datagram socket, we can listen & send on the same socket: private DatagramSocket socket; public udpServer0() { try { socket = new DatagramSocket(INPORT); System.out.println("Server Started..."); while(true) { // Block until a datagram appears: socket.receive(dp); String rcvd = Dgram.toString(dp); System.out.println("Received :"+rcvd); } } catch(SocketException e) { System.err.println("Can't open socket"); System.exit(1); } catch(IOException e) { System.err.println("Communication error"); e.printStackTrace(); } } public static void main(String[] args) { new udpServer0(); } } ///:~