Creating UDP Server And Client

27 March 2007

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.

  1. 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.
  2. 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.

 

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

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.

del.icio.us:Creating UDP Server And Client  digg:Creating UDP Server And Client  spurl:Creating UDP Server And Client  wists:Creating UDP Server And Client  simpy:Creating UDP Server And Client  newsvine:Creating UDP Server And Client  blinklist:Creating UDP Server And Client  furl:Creating UDP Server And Client  reddit:Creating UDP Server And Client  fark:Creating UDP Server And Client  blogmarks:Creating UDP Server And Client  Y!:Creating UDP Server And Client  smarking:Creating UDP Server And Client  magnolia:Creating UDP Server And Client  segnalo:Creating UDP Server And Client  gifttagging:Creating UDP Server And Client

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Creating UDP Server And Client

One Response to “Creating UDP Server And Client”

  1. mar Says:

    very usfull but the two codes are the same for the server!!!
    I cannot see the client code

Leave a Reply