覆盆子Ad-hoc U广播

时间:2014-08-10 07:41:28

标签: java linux network-programming raspberry-pi adhoc

我在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();
        }

    }

1 个答案:

答案 0 :(得分:0)

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