multicastSocket实现单播组播 局域网通信

@Event(value = R.id.test2)
private void test2(View w) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            service();
        }
    }).start();
}

@Event(value = R.id.test3)
private void test3(View w) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            getReceiver();
        }
    }).start();
}

@Event(value = R.id.test4)
private void test4(View w) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            registSocket();
        }
    }).start();
}

public void getReceiver() {
    if (!multicastSocket.isClosed() && null != multicastSocket) {
        try {
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "test444";
            byte[] buf2 = time.getBytes();
            //192.168.1.101s
            InetAddress byName = null;
            byName = InetAddress.getByName("192.168.1.126");
            DatagramPacket datagramPacket = new DatagramPacket(buf2, buf2.length, byName, 10000);
            System.out.println("数据发送。。datagramPacket");
            multicastSocket.send(datagramPacket);
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

MulticastSocket multicastSocket;

public void registSocket() {
    if (ip != null) {
        System.out.println("ip:" + ip);
        try {
            if (!multicastSocket.isClosed() && null != multicastSocket) {
                String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "test333";
                byte[] buf2 = time.getBytes();
                //192.168.1.101s
                InetAddress byName = InetAddress.getByName(ip.getHostAddress().toString());
                DatagramPacket datagramPacket = new DatagramPacket(buf2, buf2.length, byName, 10000);
                System.out.println("数据发送。。datagramPacket");
                multicastSocket.send(datagramPacket);
                Thread.sleep(1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public void service() {
    // TODO Auto-generated method stub

    try {
        multicastSocket = new MulticastSocket(10000);
        InetAddress address = InetAddress.getByName("239.0.0.1");
        multicastSocket.joinGroup(address);
        byte[] buf = new byte[1024];
        while (true) {
            DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
            multicastSocket.receive(datagramPacket); // 接收数据,同样会进入阻塞状态
            byte[] message = new byte[datagramPacket.getLength()]; // buffer中截取收到的数据
            System.arraycopy(buf, 0, message, 0, datagramPacket.getLength());
            ip = datagramPacket.getAddress();
            System.out.println(ip);
            System.out.println(new String(message));
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // 接收数据时需要指定监听的端口号
}

@Event(value = R.id.test1)
private void test(View w) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            sent();
        }
    }).start();
}

public void sent() {
    MulticastSocket multicastSocket;
    try {
        multicastSocket = new MulticastSocket();
        InetAddress address = InetAddress.getByName("239.0.0.1"); // 必须使用D类地址
        multicastSocket.joinGroup(address); // D类地址为标识,加入同一个组才能实现广播
        multicastSocket.setLoopbackMode(false);
        while (true) {//心跳包
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "test1";
            byte[] buf = time.getBytes();
            DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
            datagramPacket.setAddress(address); // 接收地址和group的标识相同
            datagramPacket.setPort(10000); // 发送至的端口号
            multicastSocket.send(datagramPacket);
            Thread.sleep(1000);
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
 
 

你可能感兴趣的:(multicastSocket实现单播组播 局域网通信)