Saturday, July 17, 2010

Socket Programming with UDP in Java

Continuation to my post on TCP socket program, this is an example of using the UDP protocol at the Transport layer to implement the same process. The code is available here.

Most of the concepts covered in my earlier post on TCP is repeated here. But there are some striking difference between TCP and UDP which are summarized below.

1) UDP is connection-less - There is no handshaking before sending the datagrams.

2) It is message oriented - Each packet contains the information about the destination (IP address and the port number). UDP doesn't give guarantees delivery of the packet (unreliable) or the order in which the packets are delivered. (Contrast this with the reliable data transfer of TCP and the byte-streams model of data transfer between the client and server).

3) In the text (Computer Networking: A top down approach), the authors have compared the UDP to a taxi-service. Each taxi driver must be informed about the destination of the taxi (in UDP - IP and port).

The source code is extensively commented and you might not have difficulties understanding it.

The Client

/*
* Coded by: Rahul Krishnan
*
* Description: A UDP based client which gets input form
* the user and sends the sentence to the server. It
* receives the capitalized form of the sentence and
* prints it on standard output.
*/

import java.io.*;
import java.net.*;

class UDPClient {
public static void main(String argv[]) throws Exception {
String sentence; // User input sentence.
String modifiedSentence; // Capitalized sentence from server.

// Bind to the standard input stream.
BufferedReader fromUser = new BufferedReader(
new InputStreamReader(System.in));

// Initializing the client socket.
DatagramSocket clientSocket = new DatagramSocket();

InetAddress IPAddress = InetAddress.getByName("localhost");

byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];

System.out.print("Send Msg> ");

sentence = fromUser.readLine();
sendData = sentence.getBytes();

// Initialize the datagram packet to be send.
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 4444);

// Send the datagram packet through the socket.
clientSocket.send(sendPacket);

// Initialize a datagram packet to received the server reply.
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

clientSocket.receive(receivePacket);
modifiedSentence = new String(receivePacket.getData());

System.out.println("Server reply> " + modifiedSentence);
clientSocket.close();
}
}


The Server

/*
* Coded by: Rahul Krishnan
*
* Description: A UDP based server which takes in
* packets from client and the sends back the
* capitalized form of the sentence to the client.
*/

import java.net.*;

class UDPServer {
public static void main(String argv[]) throws Exception {
String recSentence; // Sentence received from client.
String capSentence; // Capitalized sentence.

DatagramSocket serverSocket = new DatagramSocket(4444);

byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];

DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

while (true) {
System.out.println("Waiting for packets...");
serverSocket.receive(receivePacket);

recSentence = new String(receivePacket.getData());
System.out.println("Client Sent> " + recSentence);

// Extract the client IP address from the packet.
InetAddress clientAddress = receivePacket.getAddress();
int port = receivePacket.getPort();

capSentence = recSentence.toUpperCase();
sendData = capSentence.getBytes();

DatagramPacket sendPacket = new DatagramPacket(
sendData, sendData.length, clientAddress, port);
serverSocket.send(sendPacket);
}
}
}

No comments:

Post a Comment