Selected Reading
Java.net.DatagramPacket.setAddress() Method
Description
The java.net.DatagramPacket.setAddress(InetAddress iaddr) sets the IP address of the machine to which this datagram is being sent.
Declaration
Following is the declaration for java.net.DatagramPacket.setAddress() method
public void setAddress(InetAddress iaddr)
Parameters
iaddr - the InetAddress
Return Value
This method does not return any value.
Exception
NA
Example
The following example shows the usage of net.DatagramPacket.setAddress() method
package com.tutorialspoint;
import java.net.*;
class DatagramPacketDemo {
public static void main(String args[]) throws Exception {
// create a DatagramSocket object
DatagramSocket ds = new DatagramSocket();
// create a byte array buf
byte buf[] = new byte[100];
// create a DatagramPacket object
DatagramPacket dp = new DatagramPacket(buf, buf.length);
// create InetAddress object ia and assign destination address
InetAddress ia = InetAddress.getByName("localhost");
// set ia as destination to datagram packet dp
dp.setAddress(ia);
System.out.println("Destination address is set to " + ia );
}
}
Let us compile and run the above program, this will produce the following result:
Destination address is set to localhost/127.0.0.1
Advertisements