Advertisement

Udemy WW Udemy WW

Friday, 9 December 2011

Sending and Receiving DatagramPacket in java("implementation Of UDP Protocol")

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

class Sender
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds=new DatagramSocket();
String s=hello!!! this is data"";
byte b[]=s.getBytes();
DatagramPacket dp=new DatagramPacket(b,b.length,InetAddress.getLocalHost(),8);
ds.send(dp);
}
}


Receiver

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

class Receiver
{
public static void main(String[] sd) throws Exception
{
DatagramSocket ds=new DatagramSocket(8);
byte b[]=new byte[1024];
DatagramPacket dp=new DatagramPacket(b,b.length,InetAddress.getLocalHost(),8);
ds.receive(dp);
String r=new String(dp.getData());
System.out.println("hi..........");
System.out.println("DATA"+r.trim());
}
}

No comments:

Post a Comment

Advertisement

Udemy WW