InetAddress
and port to which you wish to send it into the DatagramPacket()
constructor. For example,
try {
InetAddress metalab = new InetAddess("metalab.unc.edu");
int chargen = 19;
String s = "My second UDP Packet";
byte[] b = s.getBytes();
DatagramPacket dp = new DatagramPacket(b, b.length, metalab, chargen);
}
catch (UnknownHostException e) {
System.err.println(e);
}
Next you create a DatagramSocket
object and pass the packet to its
send() method:
For example,
try {
DatagramSocket sender = new DatagramSocket();
sender.send(dp);
}
catch (IOException e) {
System.err.println(e);
}