Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A <code>DatagramSocket</code> remains bound when disconnected, it is the <code>close()</code> method that would unbind it. Note that for a UDP (datagram) socket the semantics of <code>connect()</code> and <code>disconnect()</code> are different as compared to a TCP (or other connection oriented) socket. UDP is a connectionless protocol and a bound <code>DatagramSocket</code> can send and receive packets without being 'connected'. The <code>connect()</code> method has a purely local effect in that it causes the socket to only be able to send and receive packets to a given host/port, i.e. acting as a filter. A <code>DatagramSocket</code> connected to a multicast or broadcast address will only be able to send packets and not receive them.</p> <p><code>bind(SocketAddress)</code> is used to attach a socket to a local address/port combination, before a socket is bound it cannot receive or send any packets. The default behaviour of the constructors is to bind the socket immediately. To create an unbound 'DatagramSocket' use the <code>DatagramSocket(SocketAddress)</code> constructor passing <code>null</code> as an argument. Then it is possible to apply any custom configuration to the socket before binding it with <code>bind()</code>.</p> <p>As far as I know an open <code>DatagramSocket</code> that goes out of scope will cause a resource leak, the object may be garbage collected, but I'm pretty sure the underlying UDP socket will remain allocated by the OS until the JVM process exits.</p> <p>If an address is not specified before the socket is bound, when bound it will attach to the wildcard address (<a href="http://www.cs.cmu.edu/~srini/15-441/F01.full/www/assignments/P2/htmlsim_split/node18.html"><code>INADDR_ANY</code></a>), making it able to receive and send packets from any available local address (unless it is later 'connected' to some host). If a port is not specified (or specified as 0) then the socket is bound to some available port chosen by the OS (<a href="http://en.wikipedia.org/wiki/Ephemeral_port">ephemeral port</a>).</p> <p>Edit: An example</p> <pre><code>// bind to INADDR_ANY, allowing packets on all IP addresses of host: DatagramSocket dsock = new DatagramSocket(55555); DatagramPacket packet = new DatagramPacket(new byte[8192]); //next packet can be from anywhere including local host (127.0.0.0/8) dsock.receive(packet); // restrict communication to a single host/port: dsock.connect(new InetSocketAddress("somehost.net", 99)); // next packet can only be from somehost.net on port 99: dsock.receive(packet); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload