Subscribe For Free Updates!

We'll not spam mate! We promise.

Oct 17, 2016

UDP Client Server example in Java

Views:

There are two types of Internet Protocol (IP) traffic. They are TCP or Transmission Control Protocol and UDP or User Datagram Protocol.

Both has their own advantages and disadvantages. You can use TCP or UDP according to criteria or problem that you want resolves.
 Today our focus to discuss UDP and at end of post you can find a simple java program that implements UDP communication between client and Server.
  • User Datagram Protocol or Universal Datagram Protocol (UDP) is connectionless protocol. Mean that either the connection is established or not, Client is alive or not it will send the data each case.
  • UDP is suitable for applications that need fast, efficient transmission, such as games, live streaming etc.
  • Following is the list of protocol that is built using UDP :
    DNS, DHCP, TFTP, SNMP, RIP, VOIP.
  • UDP has no inherent order as all packets are independent of each other. If ordering is required, it has to be managed by the application layer.
  • UDP is faster than TCP because TCP requires to first establish connection TCP also taking care of other (thing like Flow control, Congestion control etc) during transmission which normally UDP do not care so it is Fast.
  • There is no guarantee that the messages or packets sent would reach at all.
  • UDP Header size is 8 bytes.
  • UDP is lightweight. There is no ordering of messages, no tracking connections, etc. It is a small transport layer designed on top of IP.
  • UDP does not have an option for flow control.
  • UDP has No Acknowledgment segment.
  • UDP has No handshake (connectionless protocol).
Let See how we implement in java.
Client Side:
Client simple input UDP server information IP, Port, and his query. In this case, it will ask server Who are you ? this message is concatenated with ClientName : Query. see the code below
public static String UPDRequester(String requester, String ip, int port, String query) { String reply = ""; try { DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName(ip); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String request = requester + ":" + query; sendData = request.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); clientSocket.close(); reply = modifiedSentence.trim(); } catch (Exception ex) { reply = "Error: encouter, Message: " + ex.getMessage(); ex.printStackTrace(); } return reply; }
Server Side:
On the server, side Client parses this message and reply back to Client "This is a UDP Server example".
try { serverSocket = new DatagramSocket(UDPPort); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; System.out.println(ServerIP + " UDP Server Is UP!"); while (true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); String request = new String(receivePacket.getData()); String[] requestArray = request.trim().split(":"); String remoteClient = requestArray[0]; String query = requestArray[1]; System.out.println("SEVER: Request RECEIVED from "+remoteClient+", for Query " + query+ "."); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = "This is an UDP Server example."; sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } } catch (Exception ex) { ex.printStackTrace(); }
You can download complete working code from Git repository: The application is working in this way read
  1. First, you need to start the server.
  2. Server main function first read configuration Server IP, UDP port.
  3. After this Server will be on listening mode to entertain Client request.
  4. The server will completely run in a Thread.
  5. The client also first read Server information from a file and then request the
    server for queries.
 Code :  https://github.com/sajjad037/UDPClientServerExample.git

Plese Feel Free to Socializer This Post
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

1 comments:

  1. Your Affiliate Money Printing Machine is ready -

    And making profit with it is as simple as 1...2...3!

    This is how it works...

    STEP 1. Tell the system which affiliate products the system will advertise
    STEP 2. Add push button traffic (this LITERALLY takes 2 minutes)
    STEP 3. Watch the system explode your list and sell your affiliate products all on it's own!

    Are you ready to make money ONLINE??

    The solution is right here

    ReplyDelete

Become a Fan

visual studio learn