Creating UDP Server And Client
27 March 2007UDP 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.
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.
Related Posts:
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
very usfull but the two codes are the same for the server!!!
I cannot see the client code