我的Android设备连接到我的家庭无线网络。还连接了一个特殊的UDP设备。我的Android应用程序成功可以发送命令到UDP设备。但是,如果我打开一个套接字,它就不会收到数据。你能看出什么问题吗?我知道正在运行的iphone-APP中的UDP设备的ip
以下是该应用程序发送命令的方式:
public static final String SERVERIP = "192.168.2.114";
public static final int PORT = 44444;
public void run() {
try {
serverAddr = InetAddress.getByName(SERVERIP);
DatagramSocket socket = new DatagramSocket();
byte[] buf = message.toByteArray();
DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, PORT);
socket.send(packet);
socket.close();
} catch (Exception e) {
Log.e("UDP", "Sender/Client: Error", e);
}
}
我有两种接收数据的方法:
public static final String SERVERIP = "192.168.2.114";
public static final int SERVERPORT = 44445;
private InetAddress serverAddr;
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddr);
byte[] buf = new byte[65213];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
} catch (Exception e) {
Log.e("UDP", "Receiver: Error", e);
}
try {
serverAddr = InetAddress.getByName(SERVERIP);
DatagramChannel channel = DatagramChannel.open();
DatagramSocket socket = channel.socket();
byte[] buf = new byte[65213];
DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, SERVERPORT);
socket.receive(packet);
socket.close();
} catch (Exception e) {
Log.e("UDP", "Sender/Client: Error", e);
}
}
第一个试块中的方法导致异常:
java.net.BindException: Cannot assign requested address
at org.apache.harmony.luni.platform.OSNetworkSystem.bind(Native Method)
at dalvik.system.BlockGuard$WrappedNetworkSystem.bind(BlockGuard.java:268)
at org.apache.harmony.luni.net.PlainDatagramSocketImpl.bind(PlainDatagramSocketImpl.java:81)
at java.net.DatagramSocket.createSocket(DatagramSocket.java:193)
at java.net.DatagramSocket.<init>(DatagramSocket.java:95)
at de.myappname.connection.Receiver.run(Receiver.java:29)
at java.lang.Thread.run(Thread.java:1019)
第二种方法只是通过socket.receive(数据包)来阻塞线程,它不接收数据。从iphone和规范我知道设备通过UDP通过UDP 44445发送数据。有什么建议有什么不对吗? 谢谢!
答案 0 :(得分:1)
eMule协议使用UDP端口44445。您的设备上是否还有其他eMule客户端?
<强>更新强>
问题似乎是您绑定的地址 - 它必须是localhost上的地址,即设备的IP地址,而不是远程设备。请参阅DatagramSocket(port, InetAddress)
构造函数。
答案 1 :(得分:0)
我猜你需要将receive()函数放在while循环中,因为当前代码看起来只接收一次消息;它不保证它将包含任何有效数据。