In distributed applications model network/socket programming plays an vital role in exchanging the data.
We can write socket program in two ways ,One through TCP and another one through UDP. We are going to see about UDP communication.
This model is less network overhead than TCP.

We need to create server socket to send/receive the data. We can create server socket as like below.
DatagramSocket socket = new DatagramSocket(9500);

There is no need for port in sender and its only required in receiver side. Since i am combining sender and receiver i have created in generic way.Data is transmitted through data gram pocket which can be defined as like below. Datagram pocket contains the information about destination as well.Thats why IP address is required.Since i am running in localhost creating IP instance based on localhost.

String data="Hello Socket";
InetAddress ip = InetAddress.getByName("localhost");
DatagramPacket dp = new DatagramPacket(data.getBytes(), data.length(), ip, 9500);
socket.send(dp);


In the receiver side we need to instantiate same server socket and receive the data using data gram pocket.

byte[] arr= new byte[1024];
 DatagramPacket dp = new DatagramPacket(arr,arr.length);
 try {
     socket.receive(dp);
 } catch (IOException e) {
     e.printStackTrace();
 }
 String data = new String(dp.getData()).substring(0, dp.getLength());

Refer the complete running sample code below for example.

package com;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class ServerSocket implements Runnable{

   static DatagramSocket socket = null;
    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
 
 String test="Hello Socket";
 socket = new DatagramSocket(9500);
 InetAddress ip = InetAddress.getByName("localhost");
 DatagramPacket dp = new DatagramPacket(test.getBytes(), test.length(), ip, 9500);
 socket.send(dp);
 System.out.println("Sending done");
 ServerSocket st= new ServerSocket();
 new Thread(st).start();
    }
    @Override
    public void run() {
 byte[] arr= new byte[1024];
 DatagramPacket dp = new DatagramPacket(arr,arr.length);
 try {
     socket.receive(dp);
 } catch (IOException e) {
     e.printStackTrace();
 }
 String data = new String(dp.getData()).substring(0, dp.getLength());
      System.out.println("Running Thread "+data);
    }

}


Output:
Sending done
Running ThreadHello Socket