安卓:java.net.DatagramSocket.bind:无效的参数异常 [英] Android: java.net.DatagramSocket.bind: Invalid Argument Exception
问题描述
背景:我正在写一个简单的UDP的应用程序来ping一个测试服务器我管理的每分钟左右,告诉我它仍然是正常运行(我不能启用ping服务器对于那些想知道的)。我打算在我的手机上运行该警告我当服务器不再响应。
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.
我试图用看似简单的java.net.DatagramSocket中这样:
I'm trying to use the seemingly simple java.net.DatagramSocket as such:
try
{
socket = new DatagramSocket();
socket.bind(null);
}
catch (SocketException e)
{
System.out.println(e.toString());
throw e;
}
我还要说,我已经通过Android清单使互联网的权限,如果删除了使用条款的话,我得到一个权限错误,所以我敢肯定这是工作确定。当我下载这个code到Android虚拟设备(AVD)并执行它,在调用bind()我psented与此异常$ 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:
03-17 19:07:39.401:信息/的System.out(338):java.net.BindException:无效的参数
03-17 19:07:39.401: INFO/System.out(338): java.net.BindException: Invalid argument
据<一href="http://developer.android.com/reference/java/net/DatagramSocket.html#bind%28java.net.SocketAddress%29"相对=nofollow>这个文档,空的说法是正确的:
According to this documentation, the null argument is correct:
公共无效的bind(SocketAddress的localAddr)
public void bind (SocketAddress localAddr)
自:API级别1
将此套接字绑定到由localAddr指定的本地地址和端口。 如果这个值是一个有效的本地地址空任何空闲的端口用于
Binds this socket to the local address and port specified by localAddr. If this value is null any free port on a valid local address is used.
但不信任的文档,我决定列举我的设备上的IP地址是这样的:
But not trusting documentation, I decided to enumerate the IP addresses on my device like this:
ArrayList<NetworkInterface> 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步骤,它的伟大工程,我可以看到在AVD的两个IP地址,但我得到的bind()的调用完全相同的例外。是否有人在那里看到什么我可能会丢失?我将继续研究,希望张贴解决我自己的问题,但我希望有人在那里将能快捷这对我来说。
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.
推荐答案
我发现这个问题。这是我声明似乎使问题DatagramSocket的方式。如果我使用一个DatagramChannel以下列方式再打开DatagramSocket类的bind()的调用成功。
I found the problem. It is the way I'm declaring the DatagramSocket that appears to cause problems. If I use a DatagramChannel to open the DatagramSocket in the following way then the bind() call is successful.
DatagramChannel channel = DatagramChannel.open();
DatagramSocket socket = channel.socket();
这篇关于安卓:java.net.DatagramSocket.bind:无效的参数异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!