- java.lang.Object
-
- java.net.DatagramPacket
-
public final class DatagramPacket extends Object
This class represents a datagram packet.Datagram packets are used to implement a connectionless packet delivery service. Each message is routed from one machine to another based solely on information contained within that packet. Multiple packets sent from one machine to another might be routed differently, and might arrive in any order. Packet delivery is not guaranteed.
- Since:
- JDK1.0
-
-
Constructor Summary
Constructors Constructor and Description DatagramPacket(byte[] buf, int length)
Constructs aDatagramPacket
for receiving packets of lengthlength
.DatagramPacket(byte[] buf, int length, InetAddress address, int port)
Constructs a datagram packet for sending packets of lengthlength
to the specified port number on the specified host.DatagramPacket(byte[] buf, int offset, int length)
Constructs aDatagramPacket
for receiving packets of lengthlength
, specifying an offset into the buffer.DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port)
Constructs a datagram packet for sending packets of lengthlength
with offsetioffset
to the specified port number on the specified host.DatagramPacket(byte[] buf, int offset, int length, SocketAddress address)
Constructs a datagram packet for sending packets of lengthlength
with offsetioffset
to the specified port number on the specified host.DatagramPacket(byte[] buf, int length, SocketAddress address)
Constructs a datagram packet for sending packets of lengthlength
to the specified port number on the specified host.
-
Method Summary
Methods Modifier and Type Method and Description InetAddress
getAddress()
Returns the IP address of the machine to which this datagram is being sent or from which the datagram was received.byte[]
getData()
Returns the data buffer.int
getLength()
Returns the length of the data to be sent or the length of the data received.int
getOffset()
Returns the offset of the data to be sent or the offset of the data received.int
getPort()
Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received.SocketAddress
getSocketAddress()
Gets the SocketAddress (usually IP address + port number) of the remote host that this packet is being sent to or is coming from.void
setAddress(InetAddress iaddr)
Sets the IP address of the machine to which this datagram is being sent.void
setData(byte[] buf)
Set the data buffer for this packet.void
setData(byte[] buf, int offset, int length)
Set the data buffer for this packet.void
setLength(int length)
Set the length for this packet.void
setPort(int iport)
Sets the port number on the remote host to which this datagram is being sent.void
setSocketAddress(SocketAddress address)
Sets the SocketAddress (usually IP address + port number) of the remote host to which this datagram is being sent.
-
-
-
Constructor Detail
-
DatagramPacket
public DatagramPacket(byte[] buf, int offset, int length)
Constructs aDatagramPacket
for receiving packets of lengthlength
, specifying an offset into the buffer.The
length
argument must be less than or equal tobuf.length
.- Parameters:
buf
- buffer for holding the incoming datagram.offset
- the offset for the bufferlength
- the number of bytes to read.- Since:
- 1.2
-
DatagramPacket
public DatagramPacket(byte[] buf, int length)
Constructs aDatagramPacket
for receiving packets of lengthlength
.The
length
argument must be less than or equal tobuf.length
.- Parameters:
buf
- buffer for holding the incoming datagram.length
- the number of bytes to read.
-
DatagramPacket
public DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port)
Constructs a datagram packet for sending packets of lengthlength
with offsetioffset
to the specified port number on the specified host. Thelength
argument must be less than or equal tobuf.length
.- Parameters:
buf
- the packet data.offset
- the packet data offset.length
- the packet data length.address
- the destination address.port
- the destination port number.- Since:
- 1.2
- See Also:
InetAddress
-
DatagramPacket
public DatagramPacket(byte[] buf, int offset, int length, SocketAddress address) throws SocketException
Constructs a datagram packet for sending packets of lengthlength
with offsetioffset
to the specified port number on the specified host. Thelength
argument must be less than or equal tobuf.length
.- Parameters:
buf
- the packet data.offset
- the packet data offset.length
- the packet data length.address
- the destination socket address.- Throws:
IllegalArgumentException
- if address type is not supportedSocketException
- Since:
- 1.4
- See Also:
InetAddress
-
DatagramPacket
public DatagramPacket(byte[] buf, int length, InetAddress address, int port)
Constructs a datagram packet for sending packets of lengthlength
to the specified port number on the specified host. Thelength
argument must be less than or equal tobuf.length
.- Parameters:
buf
- the packet data.length
- the packet length.address
- the destination address.port
- the destination port number.- See Also:
InetAddress
-
DatagramPacket
public DatagramPacket(byte[] buf, int length, SocketAddress address) throws SocketException
Constructs a datagram packet for sending packets of lengthlength
to the specified port number on the specified host. Thelength
argument must be less than or equal tobuf.length
.- Parameters:
buf
- the packet data.length
- the packet length.address
- the destination address.- Throws:
IllegalArgumentException
- if address type is not supportedSocketException
- Since:
- 1.4
- See Also:
InetAddress
-
-
Method Detail
-
getAddress
public InetAddress getAddress()
Returns the IP address of the machine to which this datagram is being sent or from which the datagram was received.- Returns:
- the IP address of the machine to which this datagram is being sent or from which the datagram was received.
- See Also:
InetAddress
,setAddress(java.net.InetAddress)
-
getPort
public int getPort()
Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received.- Returns:
- the port number on the remote host to which this datagram is being sent or from which the datagram was received.
- See Also:
setPort(int)
-
getData
public byte[] getData()
Returns the data buffer. The data received or the data to be sent starts from theoffset
in the buffer, and runs forlength
long.- Returns:
- the buffer used to receive or send data
- See Also:
setData(byte[], int, int)
-
getOffset
public int getOffset()
Returns the offset of the data to be sent or the offset of the data received.- Returns:
- the offset of the data to be sent or the offset of the data received.
- Since:
- 1.2
-
getLength
public int getLength()
Returns the length of the data to be sent or the length of the data received.- Returns:
- the length of the data to be sent or the length of the data received.
- See Also:
setLength(int)
-
setData
public void setData(byte[] buf, int offset, int length)
Set the data buffer for this packet. This sets the data, length and offset of the packet.- Parameters:
buf
- the buffer to set for this packetoffset
- the offset into the datalength
- the length of the data and/or the length of the buffer used to receive data- Throws:
NullPointerException
- if the argument is null- Since:
- 1.2
- See Also:
getData()
,getOffset()
,getLength()
-
setAddress
public void setAddress(InetAddress iaddr)
Sets the IP address of the machine to which this datagram is being sent.- Parameters:
iaddr
- theInetAddress
- Since:
- JDK1.1
- See Also:
getAddress()
-
setPort
public void setPort(int iport)
Sets the port number on the remote host to which this datagram is being sent.- Parameters:
iport
- the port number- Since:
- JDK1.1
- See Also:
getPort()
-
setSocketAddress
public void setSocketAddress(SocketAddress address)
Sets the SocketAddress (usually IP address + port number) of the remote host to which this datagram is being sent.- Parameters:
address
- theSocketAddress
- Throws:
IllegalArgumentException
- if address is null or is a SocketAddress subclass not supported by this socket- Since:
- 1.4
- See Also:
getSocketAddress()
-
getSocketAddress
public SocketAddress getSocketAddress()
Gets the SocketAddress (usually IP address + port number) of the remote host that this packet is being sent to or is coming from.- Returns:
- the
SocketAddress
- Since:
- 1.4
- See Also:
setSocketAddress(java.net.SocketAddress)
-
setData
public void setData(byte[] buf)
Set the data buffer for this packet. With the offset of this DatagramPacket set to 0, and the length set to the length ofbuf
.- Parameters:
buf
- the buffer to set for this packet.- Throws:
NullPointerException
- if the argument is null.- Since:
- JDK1.1
- See Also:
getLength()
,getData()
-
setLength
public void setLength(int length)
Set 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. The length must be lesser or equal to the offset plus the length of the packet's buffer.- Parameters:
length
- the length to set for this packet.- Throws:
IllegalArgumentException
- if the length is negative of if the length is greater than the packet's data buffer length.- Since:
- JDK1.1
- See Also:
getLength()
,setData(byte[], int, int)
-
-
Deutsche Übersetzung
Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.
Vielen Dank im Voraus.
Dokument erstellt 11/06/2005, zuletzt geändert 04/03/2020
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/java-api-rf-java/net/datagrampacket.html
Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.
Referenzen
Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.