Source Code : Read and write with DatagramPacket
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Read and write with DatagramPacket
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class Main {
public static void main(String[] args) throws Exception {
byte[] ary = new byte[128];
DatagramPacket pack = new DatagramPacket(ary, 128);
// read
DatagramSocket sock = new DatagramSocket(1111);
sock.receive(pack);
String word = new String(pack.getData());
System.out.println("From: " + pack.getAddress() + " Port: " + pack.getPort());
System.out.println(word);
sock.close();
// write
sock = new DatagramSocket();
pack.setAddress(InetAddress.getByName(args[1]));
pack.setData(args[2].getBytes());
pack.setPort(1111);
sock.send(pack);
sock.close();
}
}
Thank with us