Documentation DatagramSocket
The User Datagram Protocol (UDP) is one of the core members of the Internet protocol suite, the set of network protocols used for the Internet. With UDP, computer applications can send messages, in this case referred to as datagrams, to other hosts on an Internet Protocol (IP) network without prior communications to set up special transmission channels or data paths. The protocol was designed by David P. Reed in 1980 and formally defined in RFC 768.
UDP uses a simple transmission model with a minimum of protocol mechanism. It has no handshaking dialogues, and thus exposes any unreliability of the underlying network protocol to the user's program. As this is normally IP over unreliable media, there is no guarantee of delivery, ordering or duplicate protection. UDP provides checksums for data integrity, and port numbers for addressing different functions at the source and destination of the datagram.
UDP is suitable for purposes where error checking and correction is either not necessary or performed in the application, avoiding the overhead of such processing at the network interface level. Time-sensitive applications often use UDP because dropping packets is preferable to waiting for delayed packets, which may not be an option in a real-time system. If error correction facilities are needed at the network interface level, an application may use the Transmission Control Protocol (TCP) or Stream Control Transmission Protocol (SCTP) which are designed for this purpose.
Reference (Wikipedia) User Datagram Protocol (UDP)
DGServer.java
// Demonstrate datagrams -- server side. import java.net.*; import java.io.*; class DGServer { // These ports were chosen arbitrarily. You must use // unused ports on your machine. public static int clientPort = 50000; public static int serverPort = 50001; public static DatagramSocket ds; public static void dgServer() throws IOException { byte[] buffer; String str; BufferedReader conin = new BufferedReader( new InputStreamReader(System.in)); System.out.println("Enter characters. Enter 'stop' to quit."); for(;;) { // read a string from the keyboard str = conin.readLine(); // convert string to byte array for transmission buffer = str.getBytes(); // send a new packet that contains the string ds.send(new DatagramPacket(buffer, buffer.length, InetAddress.getLocalHost(), clientPort)); // quit when "stop" is entered if(str.equals("stop")) { System.out.println("Server Quits."); return; } } } public static void main(String[] args) { ds = null; try { ds = new DatagramSocket(serverPort); dgServer(); } catch(IOException exc) { System.out.println("Communication error: " + exc); } finally { if(ds != null) ds.close(); } } }
DGClient.java
// Demonstrate datagrams -- client side. import java.net.*; import java.io.*; class DGClient { // This ports was choosen arbitrarily. You must use // an unused port on your machine. public static int clientPort = 50000; public static int buffer_size = 1024; public static DatagramSocket ds; public static void dgClient() throws IOException { String str; byte[] buffer = new byte[buffer_size]; System.out.println("Receiving Data"); for(;;) { // create a new packet to receive the data DatagramPacket p = new DatagramPacket(buffer, buffer.length); // wait for a packet ds.receive(p); // convert buffer into String str = new String(p.getData(), 0, p.getLength()); // display the string on the client System.out.println(str); // quit when "stop" is received. if(str.equals("stop")) { System.out.println("Client Stopping."); break; } } } public static void main(String[] args) { ds = null; try { ds = new DatagramSocket(clientPort); dgClient(); } catch(IOException exc) { System.out.println("Communication error: " + exc); } finally { if(ds != null) ds.close(); } } }
C:\ece538\java.net>java DGServer Enter characters. Enter 'stop' to quit. hello world! Are you listening? stop Server Quits.
C:\ece538\java.net>java DGClient Receiving Data hello world! Are you listening? stop Client Stopping.
Maintained by John Loomis, updated Wed Nov 14 20:09:25 2012