java.net
Class DatagramSocket

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.net.DatagramSocket
Direct Known Subclasses:
MulticastSocket sample code for java.net.MulticastSocket definition code for java.net.MulticastSocket

public class DatagramSocket
extends Object sample code for java.lang.Object definition code for java.lang.Object

This class represents a socket for sending and receiving datagram packets.

A datagram socket is the sending or receiving point for a packet delivery service. Each packet sent or received on a datagram socket is individually addressed and routed. Multiple packets sent from one machine to another may be routed differently, and may arrive in any order.

UDP broadcasts sends are always enabled on a DatagramSocket. In order to receive broadcast packets a DatagramSocket should be bound to the wildcard address. In some implementations, broadcast packets may also be received when a DatagramSocket is bound to a more specific address.

Example: DatagramSocket s = new DatagramSocket(null); s.bind(new InetSocketAddress(8888)); Which is equivalent to: DatagramSocket s = new DatagramSocket(8888); Both cases will create a DatagramSocket able to receive broadcasts on UDP port 8888.

Since:
JDK1.0
See Also:
DatagramPacket sample code for java.net.DatagramPacket definition code for java.net.DatagramPacket , DatagramChannel sample code for java.nio.channels.DatagramChannel definition code for java.nio.channels.DatagramChannel

Constructor Summary
  DatagramSocket sample code for java.net.DatagramSocket.DatagramSocket() definition code for java.net.DatagramSocket.DatagramSocket() ()
          Constructs a datagram socket and binds it to any available port on the local host machine.
protected DatagramSocket sample code for java.net.DatagramSocket.DatagramSocket(java.net.DatagramSocketImpl) definition code for java.net.DatagramSocket.DatagramSocket(java.net.DatagramSocketImpl) (DatagramSocketImpl sample code for java.net.DatagramSocketImpl definition code for java.net.DatagramSocketImpl  impl)
          Creates an unbound datagram socket with the specified DatagramSocketImpl.
  DatagramSocket sample code for java.net.DatagramSocket.DatagramSocket(int) definition code for java.net.DatagramSocket.DatagramSocket(int) (int port)
          Constructs a datagram socket and binds it to the specified port on the local host machine.
  DatagramSocket sample code for java.net.DatagramSocket.DatagramSocket(int, java.net.InetAddress) definition code for java.net.DatagramSocket.DatagramSocket(int, java.net.InetAddress) (int port, InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  laddr)
          Creates a datagram socket, bound to the specified local address.
  DatagramSocket sample code for java.net.DatagramSocket.DatagramSocket(java.net.SocketAddress) definition code for java.net.DatagramSocket.DatagramSocket(java.net.SocketAddress) (SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  bindaddr)
          Creates a datagram socket, bound to the specified local socket address.
 
Method Summary
 void bind sample code for java.net.DatagramSocket.bind(java.net.SocketAddress) definition code for java.net.DatagramSocket.bind(java.net.SocketAddress) (SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  addr)
          Binds this DatagramSocket to a specific address & port.
 void close sample code for java.net.DatagramSocket.close() definition code for java.net.DatagramSocket.close() ()
          Closes this datagram socket.
 void connect sample code for java.net.DatagramSocket.connect(java.net.InetAddress, int) definition code for java.net.DatagramSocket.connect(java.net.InetAddress, int) (InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  address, int port)
          Connects the socket to a remote address for this socket.
 void connect sample code for java.net.DatagramSocket.connect(java.net.SocketAddress) definition code for java.net.DatagramSocket.connect(java.net.SocketAddress) (SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  addr)
          Connects this socket to a remote socket address (IP address + port number).
 void disconnect sample code for java.net.DatagramSocket.disconnect() definition code for java.net.DatagramSocket.disconnect() ()
          Disconnects the socket.
 boolean getBroadcast sample code for java.net.DatagramSocket.getBroadcast() definition code for java.net.DatagramSocket.getBroadcast() ()
          Tests if SO_BROADCAST is enabled.
 DatagramChannel sample code for java.nio.channels.DatagramChannel definition code for java.nio.channels.DatagramChannel getChannel sample code for java.net.DatagramSocket.getChannel() definition code for java.net.DatagramSocket.getChannel() ()
          Returns the unique DatagramChannel sample code for java.nio.channels.DatagramChannel definition code for java.nio.channels.DatagramChannel object associated with this datagram socket, if any.
 InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress getInetAddress sample code for java.net.DatagramSocket.getInetAddress() definition code for java.net.DatagramSocket.getInetAddress() ()
          Returns the address to which this socket is connected.
 InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress getLocalAddress sample code for java.net.DatagramSocket.getLocalAddress() definition code for java.net.DatagramSocket.getLocalAddress() ()
          Gets the local address to which the socket is bound.
 int getLocalPort sample code for java.net.DatagramSocket.getLocalPort() definition code for java.net.DatagramSocket.getLocalPort() ()
          Returns the port number on the local host to which this socket is bound.
 SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress getLocalSocketAddress sample code for java.net.DatagramSocket.getLocalSocketAddress() definition code for java.net.DatagramSocket.getLocalSocketAddress() ()
          Returns the address of the endpoint this socket is bound to, or null if it is not bound yet.
 int getPort sample code for java.net.DatagramSocket.getPort() definition code for java.net.DatagramSocket.getPort() ()
          Returns the port for this socket.
 int getReceiveBufferSize sample code for java.net.DatagramSocket.getReceiveBufferSize() definition code for java.net.DatagramSocket.getReceiveBufferSize() ()
          Get value of the SO_RCVBUF option for this DatagramSocket, that is the buffer size used by the platform for input on this DatagramSocket.
 SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress getRemoteSocketAddress sample code for java.net.DatagramSocket.getRemoteSocketAddress() definition code for java.net.DatagramSocket.getRemoteSocketAddress() ()
          Returns the address of the endpoint this socket is connected to, or null if it is unconnected.
 boolean getReuseAddress sample code for java.net.DatagramSocket.getReuseAddress() definition code for java.net.DatagramSocket.getReuseAddress() ()
          Tests if SO_REUSEADDR is enabled.
 int getSendBufferSize sample code for java.net.DatagramSocket.getSendBufferSize() definition code for java.net.DatagramSocket.getSendBufferSize() ()
          Get value of the SO_SNDBUF option for this DatagramSocket, that is the buffer size used by the platform for output on this DatagramSocket.
 int getSoTimeout sample code for java.net.DatagramSocket.getSoTimeout() definition code for java.net.DatagramSocket.getSoTimeout() ()
          Retrive setting for SO_TIMEOUT.
 int getTrafficClass sample code for java.net.DatagramSocket.getTrafficClass() definition code for java.net.DatagramSocket.getTrafficClass() ()
          Gets traffic class or type-of-service in the IP datagram header for packets sent from this DatagramSocket.
 boolean isBound sample code for java.net.DatagramSocket.isBound() definition code for java.net.DatagramSocket.isBound() ()
          Returns the binding state of the socket.
 boolean isClosed sample code for java.net.DatagramSocket.isClosed() definition code for java.net.DatagramSocket.isClosed() ()
          Returns whether the socket is closed or not.
 boolean isConnected sample code for java.net.DatagramSocket.isConnected() definition code for java.net.DatagramSocket.isConnected() ()
          Returns the connection state of the socket.
 void receive sample code for java.net.DatagramSocket.receive(java.net.DatagramPacket) definition code for java.net.DatagramSocket.receive(java.net.DatagramPacket) (DatagramPacket sample code for java.net.DatagramPacket definition code for java.net.DatagramPacket  p)
          Receives a datagram packet from this socket.
 void send sample code for java.net.DatagramSocket.send(java.net.DatagramPacket) definition code for java.net.DatagramSocket.send(java.net.DatagramPacket) (DatagramPacket sample code for java.net.DatagramPacket definition code for java.net.DatagramPacket  p)
          Sends a datagram packet from this socket.
 void setBroadcast sample code for java.net.DatagramSocket.setBroadcast(boolean) definition code for java.net.DatagramSocket.setBroadcast(boolean) (boolean on)
          Enable/disable SO_BROADCAST.
static void setDatagramSocketImplFactory sample code for java.net.DatagramSocket.setDatagramSocketImplFactory(java.net.DatagramSocketImplFactory) definition code for java.net.DatagramSocket.setDatagramSocketImplFactory(java.net.DatagramSocketImplFactory) (DatagramSocketImplFactory sample code for java.net.DatagramSocketImplFactory definition code for java.net.DatagramSocketImplFactory  fac)
          Sets the datagram socket implementation factory for the application.
 void setReceiveBufferSize sample code for java.net.DatagramSocket.setReceiveBufferSize(int) definition code for java.net.DatagramSocket.setReceiveBufferSize(int) (int size)
          Sets the SO_RCVBUF option to the specified value for this DatagramSocket.
 void setReuseAddress sample code for java.net.DatagramSocket.setReuseAddress(boolean) definition code for java.net.DatagramSocket.setReuseAddress(boolean) (boolean on)
          Enable/disable the SO_REUSEADDR socket option.
 void setSendBufferSize sample code for java.net.DatagramSocket.setSendBufferSize(int) definition code for java.net.DatagramSocket.setSendBufferSize(int) (int size)
          Sets the SO_SNDBUF option to the specified value for this DatagramSocket.
 void setSoTimeout sample code for java.net.DatagramSocket.setSoTimeout(int) definition code for java.net.DatagramSocket.setSoTimeout(int) (int timeout)
          Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
 void setTrafficClass sample code for java.net.DatagramSocket.setTrafficClass(int) definition code for java.net.DatagramSocket.setTrafficClass(int) (int tc)
          Sets traffic class or type-of-service octet in the IP datagram header for datagrams sent from this DatagramSocket.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

DatagramSocket sample code for java.net.DatagramSocket() definition code for java.net.DatagramSocket()

public DatagramSocket()
               throws SocketException sample code for java.net.SocketException definition code for java.net.SocketException 
Constructs a datagram socket and binds it to any available port on the local host machine. The socket will be bound to the wildcard address, an IP address chosen by the kernel.

If there is a security manager, its checkListen method is first called with 0 as its argument to ensure the operation is allowed. This could result in a SecurityException.

Throws:
SocketException sample code for java.net.SocketException definition code for java.net.SocketException - if the socket could not be opened, or the socket could not bind to the specified local port.
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and its checkListen method doesn't allow the operation.
See Also:
SecurityManager.checkListen(int) sample code for java.lang.SecurityManager.checkListen(int) definition code for java.lang.SecurityManager.checkListen(int)

DatagramSocket sample code for java.net.DatagramSocket(java.net.DatagramSocketImpl) definition code for java.net.DatagramSocket(java.net.DatagramSocketImpl)

protected DatagramSocket(DatagramSocketImpl sample code for java.net.DatagramSocketImpl definition code for java.net.DatagramSocketImpl  impl)
Creates an unbound datagram socket with the specified DatagramSocketImpl.

Parameters:
impl - an instance of a DatagramSocketImpl the subclass wishes to use on the DatagramSocket.
Since:
1.4

DatagramSocket sample code for java.net.DatagramSocket(java.net.SocketAddress) definition code for java.net.DatagramSocket(java.net.SocketAddress)

public DatagramSocket(SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  bindaddr)
               throws SocketException sample code for java.net.SocketException definition code for java.net.SocketException 
Creates a datagram socket, bound to the specified local socket address.

If, if the address is null, creates an unbound socket.

If there is a security manager, its checkListen method is first called with the port from the socket address as its argument to ensure the operation is allowed. This could result in a SecurityException.

Parameters:
bindaddr - local socket address to bind, or null for an unbound socket.
Throws:
SocketException sample code for java.net.SocketException definition code for java.net.SocketException - if the socket could not be opened, or the socket could not bind to the specified local port.
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and its checkListen method doesn't allow the operation.
Since:
1.4
See Also:
SecurityManager.checkListen(int) sample code for java.lang.SecurityManager.checkListen(int) definition code for java.lang.SecurityManager.checkListen(int)

DatagramSocket sample code for java.net.DatagramSocket(int) definition code for java.net.DatagramSocket(int)

public DatagramSocket(int port)
               throws SocketException sample code for java.net.SocketException definition code for java.net.SocketException 
Constructs a datagram socket and binds it to the specified port on the local host machine. The socket will be bound to the wildcard address, an IP address chosen by the kernel.

If there is a security manager, its checkListen method is first called with the port argument as its argument to ensure the operation is allowed. This could result in a SecurityException.

Parameters:
port - port to use.
Throws:
SocketException sample code for java.net.SocketException definition code for java.net.SocketException - if the socket could not be opened, or the socket could not bind to the specified local port.
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and its checkListen method doesn't allow the operation.
See Also:
SecurityManager.checkListen(int) sample code for java.lang.SecurityManager.checkListen(int) definition code for java.lang.SecurityManager.checkListen(int)

DatagramSocket sample code for java.net.DatagramSocket(int, java.net.InetAddress) definition code for java.net.DatagramSocket(int, java.net.InetAddress)

public DatagramSocket(int port,
                      InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  laddr)
               throws SocketException sample code for java.net.SocketException definition code for java.net.SocketException 
Creates a datagram socket, bound to the specified local address. The local port must be between 0 and 65535 inclusive. If the IP address is 0.0.0.0, the socket will be bound to the wildcard address, an IP address chosen by the kernel.

If there is a security manager, its checkListen method is first called with the port argument as its argument to ensure the operation is allowed. This could result in a SecurityException.

Parameters:
port - local port to use
laddr - local address to bind
Throws:
SocketException sample code for java.net.SocketException definition code for java.net.SocketException - if the socket could not be opened, or the socket could not bind to the specified local port.
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and its checkListen method doesn't allow the operation.
Since:
JDK1.1
See Also:
SecurityManager.checkListen(int) sample code for java.lang.SecurityManager.checkListen(int) definition code for java.lang.SecurityManager.checkListen(int)
Method Detail

bind sample code for java.net.DatagramSocket.bind(java.net.SocketAddress) definition code for java.net.DatagramSocket.bind(java.net.SocketAddress)

public void bind(SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  addr)
          throws SocketException sample code for java.net.SocketException definition code for java.net.SocketException 
Binds this DatagramSocket to a specific address & port.

If the address is null, then the system will pick up an ephemeral port and a valid local address to bind the socket.

Parameters:
addr - The address & port to bind to.
Throws:
SocketException sample code for java.net.SocketException definition code for java.net.SocketException - if any error happens during the bind, or if the socket is already bound.
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and its checkListen method doesn't allow the operation.
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if addr is a SocketAddress subclass not supported by this socket.
Since:
1.4

connect sample code for java.net.DatagramSocket.connect(java.net.InetAddress, int) definition code for java.net.DatagramSocket.connect(java.net.InetAddress, int)

public void connect(InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  address,
                    int port)
Connects the socket to a remote address for this socket. When a socket is connected to a remote address, packets may only be sent to or received from that address. By default a datagram socket is not connected.

If the remote destination to which the socket is connected does not exist, or is otherwise unreachable, and if an ICMP destination unreachable packet has been received for that address, then a subsequent call to send or receive may throw a PortUnreachableException. Note, there is no guarantee that the exception will be thrown.

A caller's permission to send and receive datagrams to a given host and port are checked at connect time. When a socket is connected, receive and send will not perform any security checks on incoming and outgoing packets, other than matching the packet's and the socket's address and port. On a send operation, if the packet's address is set and the packet's address and the socket's address do not match, an IllegalArgumentException will be thrown. A socket connected to a multicast address may only be used to send packets.

Parameters:
address - the remote address for the socket
port - the remote port for the socket.
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the address is null, or the port is out of range.
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if the caller is not allowed to send datagrams to and receive datagrams from the address and port.
See Also:
disconnect() sample code for java.net.DatagramSocket.disconnect() definition code for java.net.DatagramSocket.disconnect() , send(java.net.DatagramPacket) sample code for java.net.DatagramSocket.send(java.net.DatagramPacket) definition code for java.net.DatagramSocket.send(java.net.DatagramPacket) , receive(java.net.DatagramPacket) sample code for java.net.DatagramSocket.receive(java.net.DatagramPacket) definition code for java.net.DatagramSocket.receive(java.net.DatagramPacket)

connect sample code for java.net.DatagramSocket.connect(java.net.SocketAddress) definition code for java.net.DatagramSocket.connect(java.net.SocketAddress)

public void connect(SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  addr)
             throws SocketException sample code for java.net.SocketException definition code for java.net.SocketException 
Connects this socket to a remote socket address (IP address + port number).

Parameters:
addr - The remote address.
Throws:
SocketException sample code for java.net.SocketException definition code for java.net.SocketException - if the connect fails
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if addr is null or addr is a SocketAddress subclass not supported by this socket
Since:
1.4
See Also:
connect(java.net.InetAddress, int) sample code for java.net.DatagramSocket.connect(java.net.InetAddress, int) definition code for java.net.DatagramSocket.connect(java.net.InetAddress, int)

disconnect sample code for java.net.DatagramSocket.disconnect() definition code for java.net.DatagramSocket.disconnect()

public void disconnect()
Disconnects the socket. This does nothing if the socket is not connected.

See Also:
connect(java.net.InetAddress, int) sample code for java.net.DatagramSocket.connect(java.net.InetAddress, int) definition code for java.net.DatagramSocket.connect(java.net.InetAddress, int)

isBound sample code for java.net.DatagramSocket.isBound() definition code for java.net.DatagramSocket.isBound()

public boolean isBound()
Returns the binding state of the socket.

Returns:
true if the socket succesfuly bound to an address
Since:
1.4

isConnected sample code for java.net.DatagramSocket.isConnected() definition code for java.net.DatagramSocket.isConnected()

public boolean isConnected()
Returns the connection state of the socket.

Returns:
true if the socket succesfuly connected to a server
Since:
1.4

getInetAddress sample code for java.net.DatagramSocket.getInetAddress() definition code for java.net.DatagramSocket.getInetAddress()

public InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  getInetAddress()
Returns the address to which this socket is connected. Returns null if the socket is not connected.

Returns:
the address to which this socket is connected.

getPort sample code for java.net.DatagramSocket.getPort() definition code for java.net.DatagramSocket.getPort()

public int getPort()
Returns the port for this socket. Returns -1 if the socket is not connected.

Returns:
the port to which this socket is connected.

getRemoteSocketAddress sample code for java.net.DatagramSocket.getRemoteSocketAddress() definition code for java.net.DatagramSocket.getRemoteSocketAddress()

public SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  getRemoteSocketAddress()
Returns the address of the endpoint this socket is connected to, or null if it is unconnected.

Returns:
a SocketAddress representing the remote endpoint of this socket, or null if it is not connected yet.
Since:
1.4
See Also:
getInetAddress() sample code for java.net.DatagramSocket.getInetAddress() definition code for java.net.DatagramSocket.getInetAddress() , getPort() sample code for java.net.DatagramSocket.getPort() definition code for java.net.DatagramSocket.getPort() , connect(SocketAddress) sample code for java.net.DatagramSocket.connect(java.net.SocketAddress) definition code for java.net.DatagramSocket.connect(java.net.SocketAddress)

getLocalSocketAddress sample code for java.net.DatagramSocket.getLocalSocketAddress() definition code for java.net.DatagramSocket.getLocalSocketAddress()

public SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  getLocalSocketAddress()
Returns the address of the endpoint this socket is bound to, or null if it is not bound yet.

Returns:
a SocketAddress representing the local endpoint of this socket, or null if it is not bound yet.
Since:
1.4
See Also:
getLocalAddress() sample code for java.net.DatagramSocket.getLocalAddress() definition code for java.net.DatagramSocket.getLocalAddress() , getLocalPort() sample code for java.net.DatagramSocket.getLocalPort() definition code for java.net.DatagramSocket.getLocalPort() , bind(SocketAddress) sample code for java.net.DatagramSocket.bind(java.net.SocketAddress) definition code for java.net.DatagramSocket.bind(java.net.SocketAddress)

send sample code for java.net.DatagramSocket.send(java.net.DatagramPacket) definition code for java.net.DatagramSocket.send(java.net.DatagramPacket)

public void send(DatagramPacket sample code for java.net.DatagramPacket definition code for java.net.DatagramPacket  p)
          throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Sends a datagram packet from this socket. The DatagramPacket includes information indicating the data to be sent, its length, the IP address of the remote host, and the port number on the remote host.

If there is a security manager, and the socket is not currently connected to a remote address, this method first performs some security checks. First, if p.getAddress().isMulticastAddress() is true, this method calls the security manager's checkMulticast method with p.getAddress() as its argument. If the evaluation of that expression is false, this method instead calls the security manager's checkConnect method with arguments p.getAddress().getHostAddress() and p.getPort(). Each call to a security manager method could result in a SecurityException if the operation is not allowed.

Parameters:
p - the DatagramPacket to be sent.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and its checkMulticast or checkConnect method doesn't allow the send.
PortUnreachableException sample code for java.net.PortUnreachableException definition code for java.net.PortUnreachableException - may be thrown if the socket is connected to a currently unreachable destination. Note, there is no guarantee that the exception will be thrown.
IllegalBlockingModeException sample code for java.nio.channels.IllegalBlockingModeException definition code for java.nio.channels.IllegalBlockingModeException - if this socket has an associated channel, and the channel is in non-blocking mode.
See Also:
DatagramPacket sample code for java.net.DatagramPacket definition code for java.net.DatagramPacket , SecurityManager.checkMulticast(InetAddress) sample code for java.lang.SecurityManager.checkMulticast(java.net.InetAddress) definition code for java.lang.SecurityManager.checkMulticast(java.net.InetAddress) , SecurityManager.checkConnect(java.lang.String, int) sample code for java.lang.SecurityManager.checkConnect(java.lang.String, int) definition code for java.lang.SecurityManager.checkConnect(java.lang.String, int)

receive sample code for java.net.DatagramSocket.receive(java.net.DatagramPacket) definition code for java.net.DatagramSocket.receive(java.net.DatagramPacket)

public void receive(DatagramPacket sample code for java.net.DatagramPacket definition code for java.net.DatagramPacket  p)
             throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Receives a datagram packet from this socket. When this method returns, the DatagramPacket's buffer is filled with the data received. The datagram packet also contains the sender's IP address, and the port number on the sender's machine.

This method blocks until a datagram is received. The length field of the datagram packet object contains the length of the received message. If the message is longer than the packet's length, the message is truncated.

If there is a security manager, a packet cannot be received if the security manager's checkAccept method does not allow it.

Parameters:
p - the DatagramPacket into which to place the incoming data.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
SocketTimeoutException sample code for java.net.SocketTimeoutException definition code for java.net.SocketTimeoutException - if setSoTimeout was previously called and the timeout has expired.
PortUnreachableException sample code for java.net.PortUnreachableException definition code for java.net.PortUnreachableException - may be thrown if the socket is connected to a currently unreachable destination. Note, there is no guarantee that the exception will be thrown.
IllegalBlockingModeException sample code for java.nio.channels.IllegalBlockingModeException definition code for java.nio.channels.IllegalBlockingModeException - if this socket has an associated channel, and the channel is in non-blocking mode.
See Also:
DatagramPacket sample code for java.net.DatagramPacket definition code for java.net.DatagramPacket , DatagramSocket sample code for java.net.DatagramSocket definition code for java.net.DatagramSocket

getLocalAddress sample code for java.net.DatagramSocket.getLocalAddress() definition code for java.net.DatagramSocket.getLocalAddress()

public InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  getLocalAddress()
Gets the local address to which the socket is bound.

If there is a security manager, its checkConnect method is first called with the host address and -1 as its arguments to see if the operation is allowed.

Returns:
the local address to which the socket is bound, or an InetAddress representing any local address if either the socket is not bound, or the security manager checkConnect method does not allow the operation
Since:
1.1
See Also:
SecurityManager.checkConnect(java.lang.String, int) sample code for java.lang.SecurityManager.checkConnect(java.lang.String, int) definition code for java.lang.SecurityManager.checkConnect(java.lang.String, int)

getLocalPort sample code for java.net.DatagramSocket.getLocalPort() definition code for java.net.DatagramSocket.getLocalPort()

public int getLocalPort()
Returns the port number on the local host to which this socket is bound.

Returns:
the port number on the local host to which this socket is bound.

setSoTimeout sample code for java.net.DatagramSocket.setSoTimeout(int) definition code for java.net.DatagramSocket.setSoTimeout(int)

public void setSoTimeout(int timeout)
                  throws SocketException sample code for java.net.SocketException definition code for java.net.SocketException 
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a call to receive() for this DatagramSocket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the DatagramSocket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

Parameters:
timeout - the specified timeout in milliseconds.
Throws:
SocketException