Recipe 16.7 UDP Datagrams


Problem

You need to use a datagram connection (UDP) instead of a stream connection (TCP).

Solution

Use DatagramSocket and DatagramPacket.

Discussion

Datagram network traffic is a kindred spirit to the underlying packet-based Ethernet and IP (Internet protocol) layers. Unlike a stream-based connection such as TCP, datagram transports such as UDP transmit each "packet," or chunk of data, as a single entity with no necessary relation to any other. A common analogy is that TCP is like talking on the telephone, while UDP is like sending postcards or maybe fax messages.

The differences show up most in error handling. Packets can, like postcards, go astray. When was the last time the postman rang your bell to tell you that the post office had lost one of several postcards it was supposed to deliver to you? It doesn't happen, right? Because they don't keep track of them. On the other hand, when you're talking on the phone and there's a noise burst like somebody yelling in the room, or even a bad connection you can ask the person at the other end to repeat what they just said.

With a stream-based connection like a TCP socket, the network transport layer handles errors for you: it asks the other end to retransmit. With a datagram transport such as UDP, you have to handle retransmission yourself. It's kind of like numbering the postcards you send so that you can go back and resend any that don't arrive a good excuse to return to your vacation spot, perhaps.

Ian's Basic Steps: UDP Client

UDP is a bit more involved, so I'll list the basic steps for generating a UDP client:

  1. Create a DatagramSocket with no arguments (the form that takes two arguments is used on the server).

  2. Optionally connect( ) the socket to an InetAddress (see Recipe 16.2) and port number.

  3. Create one or more DatagramPacket objects; these are wrappers around a byte array that contains data you want to send and is filled in with data you receive.

  4. If you did not connect( ) the socket, provide the InetAddress and port when constructing the DatagramPacket.

  5. Set the packet's length and use sock.send(packet) to send data to the server.

  6. Use sock.receive( ) to retrieve data.


Another difference is that datagram transmission preserves message boundaries. That is, if you write 20 bytes and then write 10 bytes when using TCP, the program reading from the other end will not know if you wrote one chunk of 30 bytes, two chunks of 15, or even 30 individual characters. With a DatagramSocket, you construct a DatagramPacket object for each buffer, and its contents are sent as a single entity over the network; its contents will not be mixed together with the contents of any other buffer. The DatagramPacket object has methods like getLength( ), setPort( ), and so on.

Example 16-8 is a short program that connects via UDP to the Daytime date and time server used in Recipe 16.4. Since UDP has no real notion of "connection," even services that only send you data must be contacted by sending an empty packet, which the UDP server uses to return its response.

Example 16-8. DaytimeUDP.java
public class DaytimeUDP {     /** The UDP port number */     public final static int DAYTIME_PORT = 13;     /** A buffer plenty big enough for the date string */     protected final static int PACKET_SIZE = 100;     // main program     public static void main(String[] argv) throws IOException {         if (argv.length < 1) {             System.err.println("usage: java DaytimeUDP host");             System.exit(1);         }         String host = argv[0];         InetAddress servAddr = InetAddress.getByName(host);         DatagramSocket sock = new DatagramSocket( );                  // Allocate the data buffer         byte[] buffer = new byte[PACKET_SIZE];         // The udp packet we will send and receive         DatagramPacket packet = new DatagramPacket(             buffer, PACKET_SIZE, servAddr, DAYTIME_PORT);         /* Send empty max-length (-1 for null byte) packet to server */         packet.setLength(PACKET_SIZE-1);         sock.send(packet);         Debug.println("net", "Sent request");         // Receive a packet and print it.         sock.receive(packet);         Debug.println("net", "Got packet of size " + packet.getLength( ));         System.out.print("Date on " + host + " is " +              new String(buffer, 0, packet.getLength( )));     } }

I'll run it to my server just to be sure that it works:

$ javac DaytimeUDP.java $ java DaytimeUDP  darian Date on darian is Sat Jan 27 12:42:41 2001 $



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net