Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: java.net.DatagramSocket.bind: Invalid Argument Exception
    primarykey
    data
    text
    <p>Background: I'm writing a simple UDP application to ping a beta server I manage every minute or so to tell me it is still up and running (I can't enable ping on the server for those that are wondering). I plan to run this on my phone to warn me when the server is no longer responding.</p> <p>I'm trying to use the seemingly simple java.net.DatagramSocket as such:</p> <pre><code> try { socket = new DatagramSocket(); socket.bind(null); } catch (SocketException e) { System.out.println(e.toString()); throw e; } </code></pre> <p>Let me also say that I have enabled the Internet permissions through the android manifest and if I remove the uses clause to do so, I get a permissions error so I'm sure that is working OK. When I download this code to an Android Virtual Device (AVD) and execute it, on the call to bind() I am presented with this exception:</p> <p>03-17 19:07:39.401: INFO/System.out(338): java.net.BindException: Invalid argument</p> <p>According to <a href="http://developer.android.com/reference/java/net/DatagramSocket.html#bind%28java.net.SocketAddress%29" rel="noreferrer">this</a> documentation, the null argument is correct:</p> <blockquote> <p>public void bind (SocketAddress localAddr)</p> <p>Since: API Level 1</p> <p>Binds this socket to the local address and port specified by localAddr. <strong>If this value is null any free port on a valid local address is used</strong>.</p> </blockquote> <p>But not trusting documentation, I decided to enumerate the IP addresses on my device like this:</p> <pre><code> ArrayList&lt;NetworkInterface&gt; allInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); NetworkInterface eth = allInterfaces.get(0); InetSocketAddress addr = new InetSocketAddress(eth.getInetAddresses().nextElement(), port); try { socket = new DatagramSocket(); socket.bind(addr); } catch (SocketException e) { System.out.println(e.toString()); throw e; } </code></pre> <p>When I step through the code, it works great and I can see the two IP address on the AVD but I get the exact same exception on the bind() call. Does anybody out there see what i might be missing? I will continue to research and hopefully post a solution to my own problem, but I am hoping somebody out there will be able to shortcut this for me.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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