Java Blog

 
 
 
// UDP Client


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

class UDPClient
{
    public static void main(String args[]) throws Exception
    {
        DatagramSocket dsoc=new DatagramSocket(24);
        byte buff[]=new byte[1024];
        DatagramPacket dpack=new DatagramPacket(buff,buff.length);
        dsoc.receive(dpack);
        System.out.println(new String(dpack.getData()));
    }
}


// UDP Server

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

class UDPServer
{
    public static void main(String args[]) throws Exception
    {
        DatagramSocket dsoc=new DatagramSocket(5217);
        InetAddress host=InetAddress.getLocalHost();
        String str=(new Date()).toString();
        byte buf[]=str.getBytes();        
        dsoc.send(new DatagramPacket(buf,buf.length,host,24));
        dsoc.close();
    }
}

 
 
Didn't find what you were looking for? Find more on Program of UDP datagram