Java Reference
In-Depth Information
little hard to imagine a realistic use case here. This method probably exists mostly for
parallelism with setLocalSocketAddress() .
Managing Connections
Unlike TCP sockets, datagram sockets aren't very picky about whom they'll talk to. In
fact, by default they'll talk to anyone; but this is often not what you want. For instance,
applets are only allowed to send datagrams to and receive datagrams from the applet
host. An NFS or FSP client should accept packets only from the server it's talking to. A
networked game should listen to datagrams only from the people playing the game. The
next five methods let you choose which host you can send datagrams to and receive
datagrams from, while rejecting all others' packets.
public void connect(InetAddress host, int port)
The connect() method doesn't really establish a connection in the TCP sense. However,
it does specify that the DatagramSocket will only send packets to and receive packets
from the specified remote host on the specified remote port. Attempts to send packets
to a different host or port will throw an IllegalArgumentException . Packets received
from a different host or a different port will be discarded without an exception or other
notification.
A security check is made when the connect() method is invoked. If the VM is allowed
to send data to that host and port, the check passes silently. If not, a SecurityExcep
tion is thrown. However, once the connection has been made, send() and receive()
on that DatagramSocket no longer make the security checks they'd normally make.
public void disconnect()
The disconnect() method breaks the “connection” of a connected DatagramSocket so
that it can once again send packets to and receive packets from any host and port.
public int getPort()
If and only if a DatagramSocket is connected, the getPort() method returns the remote
port to which it is connected. Otherwise, it returns -1.
public InetAddress getInetAddress()
If and only if a DatagramSocket is connected, the getInetAddress() method returns
the address of the remote host to which it is connected. Otherwise, it returns null.
public InetAddress getRemoteSocketAddress()
If a DatagramSocket is connected, the getRemoteSocketAddress() method returns the
address of the remote host to which it is connected. Otherwise, it returns null.
Search WWH ::




Custom Search