skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Wednesday, October 17, 2012

UDP Client Server Communication using Java

Posted by Raju Gupta at 12:00 PM – 1 comments
 

UDP uses a simple transmission model without implicit handshaking dialogues for providing reliability, ordering, or data integrity. Here this is illustrated using Java, by sending data (text) from one system to another using UDP, without connection establishment.

//Client Side:
------------------------------------------------------------------------------------------------------

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

class UDPClient
{
   public static void main(String args[]) throws Exception
   {
      BufferedReader inFromUser =
         new BufferedReader(new InputStreamReader(System.in));

      DatagramSocket clientSocket = new DatagramSocket();
 //Client Socket is created

      InetAddress IPAddress = InetAddress.getByName("localhost");
 //Gets the IP Address 

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

      String sentence = inFromUser.readLine();

      sendData = sentence.getBytes();
 //sends data

      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
      clientSocket.send(sendPacket);

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

      String modifiedSentence = new String(receivePacket.getData());
      System.out.println("FROM SERVER:" + modifiedSentence);
      clientSocket.close();
   }
}

//Server Side:
-------------------------------------------------------------------------------------------------------

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

class UDPServer
{
   public static void main(String args[]) throws Exception
      {
         DatagramSocket serverSocket = new DatagramSocket(9876);
  //Server Socekt Created

            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
            while(true)
               {
                  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                  serverSocket.receive(receivePacket);
                  String sentence = new String( receivePacket.getData());
                  System.out.println("RECEIVED: " + sentence);

                  InetAddress IPAddress = receivePacket.getAddress();
                  int port = receivePacket.getPort();

                  String capitalizedSentence = sentence.toUpperCase();
   //Change sentence to Capital letter
                  sendData = capitalizedSentence.getBytes();

                  DatagramPacket sendPacket =
                  new DatagramPacket(sendData, sendData.length, IPAddress, port);
                  serverSocket.send(sendPacket);
   //Send Capitalized data back to client
               }
      }
}


Labels: Java Network Example

One Response so far.

  1. Unknown says:
    March 3, 2013 at 10:47 PM

    please can u post how to send any file.. with making chunks and at receiver side the file must be merge ..... udp server to client communication using java

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger