DatagramPacket DatagramPacket(byte[], int, int, InetAddress, int) in Java

DatagramPacket(byte[], int, int, InetAddress, int): This method is available in the java.net.DatagramPacket class of Java.

Syntax:

java.net.DatagramPacket.DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port)

This method takes five arguments. This method constructs a datagram packet for sending packets of length with offset to the specified port number on the specified host.

Note: The length argument must be less than or equal to buf.length.

Parameters: Five parameters are required for this method.

buf: the packet data.

offset: the packet data offset.

length: the packet data length.

address: the destination address, or null.

port: the destination port number.

Throws:

1. IllegalArgumentException - if the length or offset is negative, or if the length plus the offset is greater than the length of the packet's given buffer,or if the port is out of range.

Approach 1: When no exception

Java

package com.DatagramPacket;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class DatagramPacket6 {
    public static void main(String[] args)
throws UnknownHostException {

        byte buf[] = { 'a', 'b', 'c', 'd', 'e' };
        int offset = 0, length = 5;
        InetAddress address = InetAddress.getLocalHost();
        int port = 100;
        DatagramPacket datagramPacket =
new DatagramPacket(buf, offset, length, address, port);

        System.out.println(datagramPacket);
    }
}

Output:

java.net.DatagramPacket@43556938


Approach 2: IllegalArgumentException 

Java

package com.DatagramPacket;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class DatagramPacket6 {
    public static void main(String[] args)
throws UnknownHostException {

        byte buf[] = { 'a', 'b', 'c', 'd', 'e' };
        int offset = 2, length = 5;
        InetAddress address = InetAddress.getLocalHost();
        int port = 100;
        DatagramPacket datagramPacket =
new DatagramPacket(buf, offset, length, address, port);

        System.out.println(datagramPacket);
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: illegal length or offset at java.base/java.net.DatagramPacket.setData(DatagramPacket.java:300) at java.base/java.net.DatagramPacket.<init>(DatagramPacket.java:137) at com.DatagramPacket.DatagramPacket6.main(DatagramPacket6.java:14)


Some other methods of DatagramPacket class

DatagramPacket(byte[], int)This method constructs a DatagramPacket for receiving packets of length.

DatagramPacket(byte[], int, int)This method constructs a DatagramPacket for receiving length packets, specifying an offset into the buffer.

DatagramPacket(byte[], int, SocketAddress)This method constructs a datagram packet for sending packets of length to the specified port number on the specified host

DatagramPacket(byte[], int, InetAddress, int)This method constructs a datagram packet for sending packets of length to the specified port number on the specified host.

DatagramPacket(byte[], int, int, SocketAddress)This method constructs a datagram packet for sending packets of length with offset to the specified port number on the specified host.

DatagramPacket(byte[], int, int, InetAddress, int)This method constructs a datagram packet for sending length packets with offset to the specified port number on the specified host.

getAddress()This method returns the IP address of the machine to which this datagram is being sent or from which the datagram was received, or null if not set.

getData()This method returns the data buffer.

getLength()This method returns the length of the data to be sent or the length of the data received.

getOffset()This method returns the offset of the data to be sent or the offset of the data received.

getPort()This method returns the port number on the remote host to which this datagram is being sent or from which the datagram was received, or 0 if not set.

getSocketAddress()This method returns the SocketAddress (usually IP address + port number) of the remote host that this packet is being sent to or is coming from.

setAddress(InetAddress)This method sets the IP address of the machine to which this datagram is being sent.

setData(byte[])This method sets the data buffer for this packet. With the offset of this DatagramPacket set to 0, and the length set to the length of buf.

setData(byte[], int, int)This method sets the data buffer for this packet. This sets the data, length, and offset of the packet.

setLength(int)This method sets the length for this packet. The length of the packet is the number of bytes from the packet's data buffer that will be sent or the number of bytes of the packet's data buffer that will be used for receiving data.

setPort(int)This method sets the port number on the remote host to which this datagram is being sent.

setSocketAddress(SocketAddress)This method sets the SocketAddress (usually IP address + port number) of the remote host to which this datagram is being sent.

No comments:

Post a Comment