gpt4 book ai didi

java - Raspberry Ad-hoc U 广播

转载 作者:太空宇宙 更新时间:2023-11-04 03:54:27 25 4
gpt4 key购买 nike

我在 Raspberry PI 上编写了一些临时应用程序。我将 PI 和我的笔记本电脑连接到 ad-hoc 网络,并编写了一个小程序来测试该网络。如果我通过 ad-hoc 网络在笔记本电脑和 Raspberry PI 之间发送单播数据包,它似乎可以工作,但当我从 Raspberry PI 向我的笔记本电脑发送广播数据包时,我发现了一些奇怪的问题。该数据包不是从Raspberry PI发送的(我使用wireshark来捕获数据包)但是,如果我从笔记本电脑向Raspberry PI发送广播数据包,PI可以接收该数据包。此外,如果我将广播数据包从 PI 发送到本地网络上的笔记本电脑,它可以正常工作。我不知道为什么。您能给我一些建议吗

wlan0     IEEE 802.11bg  ESSID:"AdhocPI"  Nickname:"<WIFI@REALTEK>"
Mode:Ad-Hoc Frequency:2.412 GHz Cell: 02:11:87:10:D7:41
Bit Rate:54 Mb/s Sensitivity:0/0
Retry:off RTS thr:off Fragment thr:off
Power Management:off
Link Quality=2/100 Signal level=2/100 Noise level=0/100
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0

lo no wireless extensions.

eth0 no wireless extensions.

这是我的 ifconfig

eth0      Link encap:Ethernet  HWaddr b8:27:eb:b8:30:ad
inet addr:169.254.98.20 Bcast:169.254.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:22291 errors:24 dropped:12 overruns:0 frame:12
TX packets:8305 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2187504 (2.0 MiB) TX bytes:1274782 (1.2 MiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:110 errors:0 dropped:0 overruns:0 frame:0
TX packets:110 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:24095 (23.5 KiB) TX bytes:24095 (23.5 KiB)

wlan0 Link encap:Ethernet HWaddr 00:13:ef:80:12:e7
inet addr:192.168.1.103 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1589 errors:0 dropped:1160 overruns:0 frame:0
TX packets:190 errors:0 dropped:11 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:604922 (590.7 KiB) TX bytes:4714 (4.6 KiB)

这是我用来监听数据包的 Java 程序

public class UDPServer {
public static void main(String args[]) {
DatagramSocket aSocket = null;
Message replyMsg = new Message(999,"reply");
System.out.println("UDPServer");
try {
aSocket = new DatagramSocket(6669);
aSocket.setBroadcast(true);

while (true) {
byte[] buffer = new byte[1000];
DatagramPacket request = new DatagramPacket(buffer,
buffer.length);
aSocket.receive(request);
Message msgReq = null;
try {
msgReq = (Message)Serializer.deserialize(request.getData());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Send back to: "+request.getAddress()+ " port "+request.getPort());

byte[]replyBytes = Serializer.serialize(replyMsg);
DatagramPacket reply = new DatagramPacket(replyBytes,
replyBytes.length, request.getAddress(),
request.getPort());
aSocket.send(reply);

System.out.println("Receive Msg : "+ msgReq.show());
}
} catch (SocketException e) {
System.out.println("Socket: " + e.getMessage());
} catch (IOException e) {
System.out.println("IO: " + e.getMessage());
} finally {
if (aSocket != null)
aSocket.close();
}
}
}

这是我发送广播数据包的 Java 程序

public class UDPClient {
public static void main(String args[]) {
DatagramSocket aSocket = null;
try {
//System.out.println("UDPClient " +getBroadcast().toString());
aSocket = new DatagramSocket(5008);
Message msgOb = new Message(1, "Hello");
byte[] m = Serializer.serialize(msgOb);
InetAddress aHost = InetAddress.getByName("192.168.1.255");

int serverPort = 6669;
System.out.println("Send data to: "+aHost.getHostAddress()+" Port "+serverPort);
DatagramPacket request = new DatagramPacket(m, m.length,
aHost, serverPort);

aSocket.setBroadcast(true);
System.out.println("Broadcast: "+aSocket.getBroadcast());
aSocket.send(request);

byte[] buffer = new byte[3000];
DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
aSocket.receive(reply);
Message msgRep = null;
try {
msgRep = (Message)Serializer.deserialize(reply.getData());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Reply: " + msgRep.show());

} catch (SocketException e) {
System.out.println("Socket: " + e.getMessage());
} catch (IOException e) {
System.out.println("IO: " + e.getMessage());
} finally {
if (aSocket != null)aSocket.close();
}

}

最佳答案

问题出在 Wifi 适配器上。只需更改具有支持 ad-hoc 模式驱动程序的 wifi 适配器即可。它会完美地工作。

关于java - Raspberry Ad-hoc U 广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25226558/

25 4 0