Java Network Programming
Datagram (UDP) Sockets
• Unlike TCP/IP sockets, datagram sockets are
connectionless
• Connection between client and server is not
maintained throughout the duration of the
dialogue
• Each datagram packet is sent as an isolated
transmission
• Datagram (UDP) sockets provide a faster means
of transmitting data than TCP/IP sockets, but
they are unreliable.
Datagram (UDP) Sockets (contd)
• The server does not create an individual Socket
object for each client
• Instead of a ServerSocket object, the server creates a
DatagramSocket object
• As does each client when it wants to send
datagram(s) to the server
• DatagramPacket objects are created and sent at
both ends, rather than simple Strings.
UDP Server
• Process involves the following nine steps
1. Create a DatagramSocket object
DatagramSocket datagramSocket =
new DatagramSocket(1234);
2.Create a buffer for incoming datagrams
byte[] buffer = new byte[256];
UDP Server (Contd)
3. Create a DatagramPacket object for the
incoming datagram
• The constructor for this object requires two
arguments:
• the previously-created byte array
• the size of this array
DatagramPacket inPacket =
new DatagramPacket(buffer, buffer.length);
UDP Server (Contd)
4. Accept an incoming datagram
datagramSocket.receive(inPacket);
5. Retrieve the sender's address and port from the
packet
InetAddress clientAddress = inPacket.getAddress();
int clientPort = inPacket.getPort();
UDP Server (contd)
6. Retrieve the data from the buffer
String message = new String(inPacket.getData(),
0,inPacket.getLength());
7. Create the response datagram
– Create a DatagramPacket object, using an overloaded
form of the constructor thattakes four arguments:
• the byte array containing the response message;
• the size of the response;
• the client's address;
• the client's port number.
UDP Server (contd)
DatagramPacket outPacket =
new DatagramPacket(response.getBytes(),
response.length(),clientAddress, clientPort);
8.Send the response datagram
datagramSocket.send(outPacket);
9.Close the DatagramSocket
datagramSocket.close();
UDP Client
• Setting up the corresponding client requires the
eight steps listed below
1. Create a DatagramSocket object
DatagramSocket datagramSocket = new DatagramSocket();
2. Create the outgoing datagram
• This step is exactly as for step 7 of the server
program
DatagramPacket outPacket =
new DatagramPacket(message.getBytes(),
message.length(), host, PORT);
UDP Client (contd)
3. Send the datagram message
datagramSocket.send(outPacket);
4. Create a buffer for incoming datagrams
byte[] buffer = new byte[256];
5.Create a DatagramPacket object for the
incoming datagrams
DatagramPacket inPacket =
new DatagramPacket(buffer, buffer.length);
UDP Client (contd)
6. Accept an incoming datagram
datagramSocket.receive(inPacket);
6. Retrieve the data from the buffer
String message = new String(inPacket.getData(),
0,inPacket.getLength());
8. Close the DatagramSocket
datagramSocket.close();

Easy Steps to implement UDP Server and Client Sockets

  • 1.
  • 2.
    Datagram (UDP) Sockets •Unlike TCP/IP sockets, datagram sockets are connectionless • Connection between client and server is not maintained throughout the duration of the dialogue • Each datagram packet is sent as an isolated transmission • Datagram (UDP) sockets provide a faster means of transmitting data than TCP/IP sockets, but they are unreliable.
  • 3.
    Datagram (UDP) Sockets(contd) • The server does not create an individual Socket object for each client • Instead of a ServerSocket object, the server creates a DatagramSocket object • As does each client when it wants to send datagram(s) to the server • DatagramPacket objects are created and sent at both ends, rather than simple Strings.
  • 4.
    UDP Server • Processinvolves the following nine steps 1. Create a DatagramSocket object DatagramSocket datagramSocket = new DatagramSocket(1234); 2.Create a buffer for incoming datagrams byte[] buffer = new byte[256];
  • 5.
    UDP Server (Contd) 3.Create a DatagramPacket object for the incoming datagram • The constructor for this object requires two arguments: • the previously-created byte array • the size of this array DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
  • 6.
    UDP Server (Contd) 4.Accept an incoming datagram datagramSocket.receive(inPacket); 5. Retrieve the sender's address and port from the packet InetAddress clientAddress = inPacket.getAddress(); int clientPort = inPacket.getPort();
  • 7.
    UDP Server (contd) 6.Retrieve the data from the buffer String message = new String(inPacket.getData(), 0,inPacket.getLength()); 7. Create the response datagram – Create a DatagramPacket object, using an overloaded form of the constructor thattakes four arguments: • the byte array containing the response message; • the size of the response; • the client's address; • the client's port number.
  • 8.
    UDP Server (contd) DatagramPacketoutPacket = new DatagramPacket(response.getBytes(), response.length(),clientAddress, clientPort); 8.Send the response datagram datagramSocket.send(outPacket); 9.Close the DatagramSocket datagramSocket.close();
  • 9.
    UDP Client • Settingup the corresponding client requires the eight steps listed below 1. Create a DatagramSocket object DatagramSocket datagramSocket = new DatagramSocket(); 2. Create the outgoing datagram • This step is exactly as for step 7 of the server program DatagramPacket outPacket = new DatagramPacket(message.getBytes(), message.length(), host, PORT);
  • 10.
    UDP Client (contd) 3.Send the datagram message datagramSocket.send(outPacket); 4. Create a buffer for incoming datagrams byte[] buffer = new byte[256]; 5.Create a DatagramPacket object for the incoming datagrams DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
  • 11.
    UDP Client (contd) 6.Accept an incoming datagram datagramSocket.receive(inPacket); 6. Retrieve the data from the buffer String message = new String(inPacket.getData(), 0,inPacket.getLength()); 8. Close the DatagramSocket datagramSocket.close();