java.net.DatagramSocket
java.lang.Object
java.net.MulticastSocket
None
JDK 1.0 or later
The DatagramSocket class implements packet-oriented, connectionless data communication. In Internet parlance, this is the User Datagram Protocol, commonly known as UDP (see RFC 768). Each packet wanders through the network, routed by its destination address. Different packets can take different paths through the network and may arrive in a different order than they were sent. Furthermore, packets are not even guaranteed to reach their destination. It is up to an application that uses DatagramSocket to determine if data is out of order or missing. While these features may seem like disadvantages of DatagramSocket, there is also some advantage to using this class. Primarily, communication using DatagramSocket is faster than Socket stream communication because of the lack of overhead involved.
public class java.net.DatagramSocket extends java.lang.Object { // Constructors public DatagramSocket(); public DatagramSocket(int port); public DatagramSocket(int port, InetAddress laddr); // New in 1.1 // Instance Methods public void close(); public InetAddress getLocalAddress(); // New in 1.1 public int getLocalPort(); public synchronized int getSoTimeout(); // New in 1.1 public synchronized void receive(DatagramPacket p); public void send(DatagramPacket p); public synchronized void setSoTimeout(int timeout); // New in 1.1 }
If any kind of socket error occurs.
If the application is not allowed to listen on the port.
This constructor creates a DatagramSocket that is bound to any available port on the local host machine.
A port number.
If any kind of socket error occurs.
If the application is not allowed to listen on the given port.
This constructor creates a DatagramSocket that is bound to the given port on the local host machine.
public DatagramSocket(int port, InetAddress laddr) throws SocketException
New as of JDK 1.1
A port number.
A local address.
If any kind of socket error occurs.
If the application is not allowed to listen on the given port on the specified host.
This constructor creates a DatagramSocket that is bound to the given port on the specified local host machine.
This method closes the socket, releasing any system resources it holds.
New as of JDK 1.1
The local address of the socket.
If the application is not allowed to retrieve the address.
This method returns the local address to which this DatagramSocket is bound.
The port number of the socket.
This method returns the local port to which this DatagramSocket is bound.
New as of JDK 1.1
The receive time-out value for the socket.
If any kind of socket error occurs.
This method returns the receive time-out value for this socket. A value of zero indicates that the socket waits indefinitely for an incoming packet, while a non-zero value indicates the number of milliseconds it waits.
public synchronized void receive(DatagramPacket p) throws IOException
The DatagramPacket that receives incoming data.
If any kind of I/O error occurs.
If the application is not allowed to receive data from the packet's source.
If a packet does not arrive before the time-out period expires.
This method receives a datagram packet on this socket. After this method returns, the given DatagramPacket contains the packet's data and length, and the sender's address and port number. If the data that was sent is longer that the given packet's data buffer, the data is truncated.
If a time-out value is specified using the setSoTimeout() method, the method either returns with the received packet or times out, throwing an InterruptedIOException. If no time-out value is specified, the method blocks until it receives a packet.
The DatagramPacket to be sent.
If any kind of I/O error occurs.
If the application is not allowed to send data to the packet's destination.
This method sends a packet from this socket. The packet data, packet length, destination address, and destination port number are specified by the given DatagramPacket.
public synchronized void setSoTimeout(int timeout) throws SocketException
New as of JDK 1.1
The new time-out value, in milliseconds, for this socket.
If any kind of socket error occurs.
This method is used to set the time-out value that is used for receive(). A non-zero value specifies the length of time, in milliseconds, that the DatagramSocket should wait for an incoming packet. A value of zero indicates that the DatagramSocket should wait indefinitely for an incoming packet. If a time-out value is needed, this method must be called before receive().
Method |
Inherited From |
Method |
Inherited From |
---|---|---|---|
clone() |
Object |
equals(Object) |
Object |
finalize() |
Object |
getClass() |
Object |
hashCode() |
Object |
notify() |
Object |
notifyAll() |
Object |
toString() |
Object |
wait() |
Object |
wait(long) |
Object |
wait(long, int) |
Object |
DatagramPacket, DatagramSocketImpl, InetAddress, InterruptedIOException, IOException, MulticastSocket, SecurityException, Socket, SocketException
This HTML Help has been published using the chm2web software. |