java.net
Class DatagramSocket
This class models a connectionless datagram socket that sends
individual packets of data across the network. In the TCP/IP world,
this means UDP. Datagram packets do not have guaranteed delivery,
or any guarantee about the order the data will be received on the
remote host.
DatagramSocket() - Initializes a new instance of
DatagramSocket that binds to
a random port and every address on the local machine.
|
DatagramSocket(int port) - Initializes a new instance of
DatagramSocket that binds to
the specified port and every address on the local machine.
|
DatagramSocket(int port, InetAddress addr) - Initializes a new instance of
DatagramSocket that binds to
the specified local port and address.
|
DatagramSocket(DatagramSocketImpl impl) - Creates a
DatagramSocket from a specified
DatagramSocketImpl instance
|
DatagramSocket(SocketAddress address) - Initializes a new instance of
DatagramSocket that binds to
the specified local port and address.
|
clone , equals , finalize , getClass , hashCode , notify , notifyAll , toString , wait , wait , wait |
DatagramSocket
public DatagramSocket()
throws SocketException
Initializes a new instance of DatagramSocket
that binds to
a random port and every address on the local machine.
SocketException
- If an error occurs.SecurityException
- If a security manager exists and
its checkListen
method doesn't allow the operation.
DatagramSocket
public DatagramSocket(int port)
throws SocketException
Initializes a new instance of DatagramSocket
that binds to
the specified port and every address on the local machine.
port
- The local port number to bind to.
SecurityException
- If a security manager exists and its
checkListen
method doesn't allow the operation.SocketException
- If an error occurs.
DatagramSocket
public DatagramSocket(int port,
InetAddress addr)
throws SocketException
Initializes a new instance of DatagramSocket
that binds to
the specified local port and address.
port
- The local port number to bind to.addr
- The local address to bind to.
SecurityException
- If a security manager exists and its
checkListen method doesn't allow the operation.SocketException
- If an error occurs.
DatagramSocket
protected DatagramSocket(DatagramSocketImpl impl)
Creates a DatagramSocket
from a specified
DatagramSocketImpl
instance
impl
- The DatagramSocketImpl
the socket will be
created from
- 1.4
DatagramSocket
public DatagramSocket(SocketAddress address)
throws SocketException
Initializes a new instance of DatagramSocket
that binds to
the specified local port and address.
address
- The local address and port number to bind to.
SecurityException
- If a security manager exists and its
checkListen
method doesn't allow the operation.SocketException
- If an error occurs.
- 1.4
close
public void close()
Closes this datagram socket.
connect
public void connect(InetAddress address,
int port)
This method connects this socket to the specified address and port.
When a datagram socket is connected, it will only send or receive
packets to and from the host to which it is connected. A multicast
socket that is connected may only send and not receive packets.
address
- The address to connect this socket to.port
- The port to connect this socket to.
IllegalArgumentException
- If address or port are invalid.SecurityException
- If the caller is not allowed to send
datagrams to or receive from this address and port.
- 1.2
disconnect
public void disconnect()
This method disconnects this socket from the address/port it was
connected to. If the socket was not connected in the first place,
this method does nothing.
- 1.2
getChannel
public DatagramChannel getChannel()
Returns the datagram channel assoziated with this datagram socket.
- The associated
DatagramChannel
object or null
- 1.4
getInetAddress
public InetAddress getInetAddress()
This method returns the remote address to which this socket is
connected. If this socket is not connected, then this method will
return null
.
- The remote address.
- 1.2
getLocalAddress
public InetAddress getLocalAddress()
Returns the local address this datagram socket is bound to.
- The local address is the socket is bound or null
- 1.1
getLocalPort
public int getLocalPort()
Returns the local port this socket is bound to.
- The local port number.
getLocalSocketAddress
public SocketAddress getLocalSocketAddress()
Returns the local SocketAddress this socket is bound to.
- The local SocketAddress or null if the socket is not bound.
- 1.4
getPort
public int getPort()
This method returns the remote port to which this socket is
connected. If this socket is not connected, then this method will
return -1.
- The remote port.
- 1.2
getReceiveBufferSize
public int getReceiveBufferSize()
throws SocketException
This method returns the value of the system level socket option
SO_RCVBUF, which is used by the operating system to tune buffer
sizes for data transfers.
- The receive buffer size.
SocketException
- If an error occurs.
- 1.2
getRemoteSocketAddress
public SocketAddress getRemoteSocketAddress()
Returns the SocketAddress of the host this socket is conneted to
or null if this socket is not connected.
- The socket address of the remote host if connected or null
- 1.4
getSendBufferSize
public int getSendBufferSize()
throws SocketException
This method returns the value of the system level socket option
SO_SNDBUF, which is used by the operating system to tune buffer
sizes for data transfers.
- The send buffer size.
SocketException
- If an error occurs.
- 1.2
getSoTimeout
public int getSoTimeout()
throws SocketException
Returns the value of the socket's SO_TIMEOUT setting. If this method
returns 0 then SO_TIMEOUT is disabled.
- The current timeout in milliseconds.
SocketException
- If an error occurs.
- 1.1
isBound
public boolean isBound()
Returns the binding state of the socket.
- True if socket bound, false otherwise.
- 1.4
isClosed
public boolean isClosed()
Checks if the datagram socket is closed.
- True if socket is closed, false otherwise.
- 1.4
isConnected
public boolean isConnected()
Returns the connection state of the socket.
- True if socket is connected, false otherwise.
- 1.4
receive
public void receive(DatagramPacket p)
throws IOException
Reads a datagram packet from the socket. Note that this method
will block until a packet is received from the network. On return,
the passed in DatagramPacket
is populated with the data
received and all the other information about the packet.
p
- A DatagramPacket
for storing the data
IOException
- If an error occurs.IllegalBlockingModeException
- If this socket has an associated
channel, and the channel is in non-blocking mode.SecurityException
- If a security manager exists and its
checkAccept method doesn't allow the receive.
send
public void send(DatagramPacket p)
throws IOException
Sends the specified packet. The host and port to which the packet
are to be sent should be set inside the packet.
p
- The datagram packet to send.
IOException
- If an error occurs.SecurityException
- If a security manager exists and its
checkMulticast or checkConnect method doesn't allow the send.IllegalBlockingModeException
- If this socket has an associated
channel, and the channel is in non-blocking mode.
setBroadcast
public void setBroadcast(boolean enable)
throws SocketException
Enables/Disables SO_BROADCAST
enable
- True if SO_BROADCAST should be enabled, false otherwise.
SocketException
- If an error occurs
- 1.4
setReceiveBufferSize
public void setReceiveBufferSize(int size)
throws SocketException
This method sets the value for the system level socket option
SO_RCVBUF to the specified value. Note that valid values for this
option are specific to a given operating system.
size
- The new receive buffer size.
SocketException
- If an error occurs.IllegalArgumentException
- If size is 0 or negative.
- 1.2
setSendBufferSize
public void setSendBufferSize(int size)
throws SocketException
This method sets the value for the system level socket option
SO_SNDBUF to the specified value. Note that valid values for this
option are specific to a given operating system.
size
- The new send buffer size.
SocketException
- If an error occurs.IllegalArgumentException
- If size is 0 or negative.
- 1.2
setSoTimeout
public void setSoTimeout(int timeout)
throws SocketException
Sets the value of the socket's SO_TIMEOUT value. A value of 0 will
disable SO_TIMEOUT. Any other value is the number of milliseconds
a socket read/write will block before timing out.
timeout
- The new SO_TIMEOUT value in milliseconds.
SocketException
- If an error occurs.
- 1.1
DatagramSocket.java -- A class to model UDP sockets
Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004
Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.