局域网下UDP广播发送与接收

1.接收udp广播的线程

private void receiverUdpBroadCast(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                byte[] buffer = new byte[1024];
                MulticastSocket server = null;
                try {
                    server = new MulticastSocket (port);
                    InetAddress ip = InetAddress.getByName(BROADCAST_IP);
                    server.joinGroup(ip);
                    final DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
                    while(true){
                        try {
                            server.receive(packet);
                            final String s = new String(packet.getData(), 0, packet.getLength(), "UTF-8");
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    str.append("收到数据:"+s+",address:"+packet.getAddress()+"\n");
//                                    Toast.makeText(MainActivity.this,"收到数据啦:"+s,Toast.LENGTH_SHORT).show();
                                }
                            });
                            System.out.println("address : " + packet.getAddress() + ", port : " + packet.getPort() + ", content : " + s);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (SocketException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally{
                    if(server != null)
                        server.close();
                }
            }
        }).start();
    }

2.发送UDP广播线程

private void sendBroadCastToCenter(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                WifiManager wifiMgr = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
                /*这里获取了IP地址,获取到的IP地址还是int类型的。*/
                int ip = wifiInfo.getIpAddress();
                /*这里就是将int类型的IP地址通过工具转化成String类型的,便于阅读
                String ips = Formatter.formatIpAddress(ip);
                */
                /*这一步就是将本机的IP地址转换成xxx.xxx.xxx.255*/
                int broadCastIP = ip | 0xFF000000;
                Log.d("bbbb","broadCastIP:"+broadCastIP);
                MulticastSocket theSocket = null;
                try {
                    String ipAddress = Formatter.formatIpAddress(broadCastIP);
                    Log.d("bbbb","ipAddress:"+ipAddress);
                    InetAddress server = InetAddress.getByName(BROADCAST_IP);
                    theSocket = new MulticastSocket(port);
                    theSocket.joinGroup(server);
                    theSocket.setLoopbackMode(false);
                    String data = "Helloaaa";
                    DatagramPacket theOutput = new DatagramPacket(data.getBytes(), data.length(), server, port);
                /*这一句就是发送广播了,其实255就代表所有的该网段的IP地址,是由路由器完成的工作*/
                    theSocket.send(theOutput);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (theSocket != null)
                        theSocket.close();
                }
            }
        }).start();

    }

实际开发中在同一局域网下,可每隔三秒可发一次udp包,接收线程先于发送线程启动

展开阅读全文

没有更多推荐了,返回首页