View RSS Feed

My Java Tips

Creating UDP Server And Client

Rate this Entry
by , 11-08-2011 at 06:29 PM (2298 Views)
UDP stands for User Datagram Protocol.

UDP does not ensure reliable delivery of all the packets, the order in which the packets would be received and it is connectionless too.

Despite all these UDP provides quick transfer of packets through low-overhead because of an minimal error-checking and correction.




Java supports UDP through two classes.

java.net.DatagramPacket
contains a wrapper for an array of bytes into which incoming data would be read or from which the out-going data would be sent.
contains the address and port number to which the packet would be sent.
java.net.DatagramSocket
DatagramSocket object is a local connection to a port that does the sending and receiving.
There is no distinction between a UDP socket and a UDP server socket.
A DatagramSocket can send to multiple, different addresses.
The address to which data goes is stored in the packet, not in the socket.



Java Code:
import java.net.*;
import java.io.*;

public class server {
	public static void main(String args[])

	{

		DatagramSocket aSocket = null;

		try

		{

			aSocket = new DatagramSocket(6788);

			String s = "Hi";

			byte[] b = s.getBytes();

			DatagramPacket d = new DatagramPacket(b, b.length);

			byte[] buffer = new byte[1000];

			while (true)

			{

				DatagramPacket request = new DatagramPacket(buffer,

				buffer.length);

				aSocket.receive(request);

				DatagramPacket reply = new DatagramPacket(d.getData(), d

				.getLength(), request.getAddress(), request.getPort());

				aSocket.send(reply);

			}

		}

		catch (SocketException e)

		{

			System.out.println("Socket: " + e.getMessage());

		}

		catch (IOException e)

		{

			System.out.println("IO: " + e.getMessage());

		}

		finally

		{

			if (aSocket != null)

				aSocket.close();

		}

	}

}
Code Explanation :We are assigning port 6788 to our UDP server on line-8 and we have placed this in the try-block since an exception is thrown in-case of this port being already used.Only the last packet created has address associated with it. When the 'reply' packet is passed to 'asocket.send()', It is transmitted to the address which was associated with the package itself. The socket has been closed finally.

For creating the UDP client, we have

Java Code:
import java.net.*;
import java.io.*;

public class server {
	public static void main(String args[])

	{

		DatagramSocket aSocket = null;

		try

		{

			aSocket = new DatagramSocket(6788);

			String s = "Hi";

			byte[] b = s.getBytes();

			DatagramPacket d = new DatagramPacket(b, b.length);

			byte[] buffer = new byte[1000];

			while (true)

			{

				DatagramPacket request = new DatagramPacket(buffer,

				buffer.length);

				aSocket.receive(request);

				DatagramPacket reply = new DatagramPacket(d.getData(), d

				.getLength(), request.getAddress(), request.getPort());

				aSocket.send(reply);

			}

		}

		catch (SocketException e)

		{

			System.out.println("Socket: " + e.getMessage());

		}

		catch (IOException e)

		{

			System.out.println("IO: " + e.getMessage());

		}

		finally

		{

			if (aSocket != null)

				aSocket.close();

		}

	}

}
Code Explanation: First, We are associating the address of the server with 'aHost' (in this case the address of the server happens to be '192.168.5.1').Then, the port number being used by the server is associated with the 'serverPort' object (this port number has to be the same as the one being used by the server). This is the simplest use of the UDP client-server. This can be used for many advanced applications such as voice-over-internet, video-transmission-over-internet etc.

Submit "Creating UDP Server And Client" to Facebook Submit "Creating UDP Server And Client" to Digg Submit "Creating UDP Server And Client" to del.icio.us Submit "Creating UDP Server And Client" to StumbleUpon Submit "Creating UDP Server And Client" to Google

Tags: None Add / Edit Tags
Categories
Network Programming

Comments