udp广播测试代码

package com.sk.irwifi;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

public class Test {

    public static DatagramSocket ds;

    public static DatagramPacket dp;

    public static Socket socket;

   static class device{
        public String mac;
        public String ip;
        public int type;
    }

    public static List<device> all = new ArrayList<>();

   public static Object obj = new Object();

    public static void main(String args[]) throws SocketException {
        ds = new DatagramSocket();

        dp = new DatagramPacket("sdsd".getBytes(), "sdsd".getBytes().length, new InetSocketAddress("255.255.255.255",'뼃'));
        System.out.println('뼃'+0);
        new Thread(){
            public void run() {
                while(true){
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    try {
                        ds.send(dp);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
//                    System.out.println("send!!!");
                }
            }
        }.start();



        new Thread(){
            public void run() {
                byte buf [] = new byte[1024];
                DatagramPacket p =new DatagramPacket(buf,1024);
                while (true){
                    try {
                        ds.receive(p);
                        int a = 1;
                        String str = new String(p.getData(), 0, p.getLength());
//                        System.out.println(new String(p.getData(),0,p.getLength()));
                        String args[] = str.split(",");
                        if (args.length == 3) {
//                            System.out.println("ip " + args[0] + "  mac: " + args[1]);


                            synchronized (obj) {
                                boolean flag = false;
                                for (device d : all) {
                                    if (d.mac.equals(args[1])) {
                                        d.ip = args[0];
                                        flag = true;
                                    }
                                }

//                                System.out.println(flag);
                                    if (!flag) {
                                        device d = new device();
                                        d.mac = args[1];
                                        d.ip = args[0];
                                        d.type = 0;
                                        all.add(d);
                                    }
                                }


                        }
                        } catch(IOException e){
                            e.printStackTrace();
                        }


                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                super.run();
                while (true){
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("read");
                    synchronized (obj) {
                        System.out.println("size :"+all.size());
                        for (device d : all) {
                            System.out.println("device mac:" + d.mac +" ip :" +d.ip + " type :"+d.type);
                            if(d.type==0){
                                Socket socket = null;
                                try {
                                    socket = new Socket();
                                    socket.connect(new InetSocketAddress(d.ip,8899),2000);
//                                    new Socket()
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                byte buf[] = new byte[1024];
                                int len = 0;
                                try {
                                    len = socket.getInputStream().read(buf);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                if(len>0){
                                    String str = new String(buf,0,len);
                                    System.out.println("1 " + str);
                                    if(str.contains("CONNECT OK")){
                                        System.out.println("Connect Ok!");
                                        try {
                                            socket.getOutputStream().write("devicetype/2".getBytes());
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                        try {
                                            len = socket.getInputStream().read(buf);
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                        if(len>0){
                                            str =  new String(buf,0,len);
                                            System.out.println("2 " +str);
                                            int index = -1;
                                            if((index = str.indexOf("devicetype/"))==0){
                                                int type = Integer.valueOf(str.substring(11));
                                                System.out.println("type "+type);
                                                d.type = type;
                                            }
                                        }
                                    }
                                }
                                if(socket!=null){
                                    try {
                                        socket.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }.start();



    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值