DISCLAMER:我知道同一主题已有问题,但下面的问题与其他问题不同。
我正在尝试在某个端口上广播数据包。
这不起作用。我的代码:(我在实际的手机上运行)
//Creating the socket (It's a class field, not a private element)
bcSock = new DatagramSocket(DEVICE_PORT, bcAddress);//bcAddress is explained below
bcSock.setBroadcast(true);
bcSock.setSoTimeout(LISTEN_TIMEOUT);
//Somewhere else in the code, creating the packet/sending it
byte[] buf = PACKET_CONTENT.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length, bcAddress, PORT);
new SendUDPTask() {
@Override
public void handleException(Exception e) {
//TODO Handle
e.printStackTrace();
}
}.execute(bcSock, dp, LISTEN_TIMEOUT);
SendUDPTask
是一个公共类,只执行此操作:sock.send(dp);
。
bcAddress
是使用此方法生成的默认广播IP:
public static InetAddress getBroadcastAddress(Activity activity) throws IOException {
WifiManager wifi = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcp = wifi.getDhcpInfo();
// handle null somehow
int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
byte[] quads = new byte[4];
for (int k = 0; k < 4; k++)
quads[k] = (byte) (broadcast >> (k * 8));
return InetAddress.getByAddress(quads);
}
这样我就会收到这个错误:
java.net.SocketException: sendto failed: EINVAL (Invalid argument)
at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:542)
at libcore.io.IoBridge.sendto(IoBridge.java:511)
at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:184)
at java.net.DatagramSocket.send(DatagramSocket.java:305)
at me.nonamesldev.smarthouse.SendUDPTask.doInBackground(SendUDPTask.java:27)
at me.nonamesldev.smarthouse.SendUDPTask.doInBackground(SendUDPTask.java:17)
(第27行是socket.send(packet)
部分。)
尝试像DatagramSocket s = new DatagramSocket(8888);
一样创建套接字也不起作用,并在NullPointerException
上发现null buffer || null address
- DatagramSocket.send
,即使文档说这应该没问题:
DatagramSocket s = new DatagramSocket(null); s.bind(new InetSocketAddress(8888));
这相当于:DatagramSocket s = new DatagramSocket(8888);
这两种情况都会创建一个能够在UDP端口8888上接收广播的DatagramSocket。
我该怎么办?我只想在Android上的某个端口上使用UDP进行广播。为什么要这么难?
提前致谢!
编辑:我也尝试过这样创建套接字:
DatagramChannel channel = DatagramChannel.open();
DatagramSocket socket = channel.socket();
bcSock.bind(new InetSocketAddress(bcAddress, PORT));
而不是产生相同错误的bcSock = new DatagramSocket(DEVICE_PORT, bcAddress);
。
答案 0 :(得分:1)
这对我有用:
bcSock = new DatagramSocket();
bcSock.setBroadcast(true);
byte[] buf = PACKET_CONTENT.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length, bcAddress, PORT);
bcSock.send(dp);
DatagramSocket ds = new DatagramSocket(PORT);
byte[] buf = new byte[PACKET_CONTENT.getBytes().length];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
答案 1 :(得分:0)
您不应尝试将套接字绑定到广播地址。使用INADDR_ANY,它在Java中是InetAddress
null
。