Aim:- Write down a program which
send Datagram Packet by DatagramSocket.
JAVA Code :-
Sender File:
import
java.lang.*;
import
java.net.*;
public class
DSender
{
public static void main(String[] args)throws
Exception {
DatagramSocket ds=new DatagramSocket();
String str="Welcome java";
InetAddress ip=InetAddress.getByName("127.0.0.1");
DatagramPacket dp=new
DatagramPacket(str.getBytes(),str.length(),ip,2510);
ds.send(dp);
ds.close();
}
}
Receiver File :
import java.lang.*;
import java.net.*;
public class DReceiver
{
public static void main(String[] args)throws Exception {
DatagramSocket ds=new DatagramSocket(2510);
byte buf[]=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,1024);
ds.receive(dp);
String str=new String(dp.getData(),0,dp.getLength());
System.out.println(str);
ds.close();
}
}
Output:-
[Receiver]
[Sender]
No comments:
Post a Comment