udp广播测试代码

来源:互联网 发布:linux用man查看 编辑:程序博客网 时间:2024/04/30 08:56
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();    }}
原创粉丝点击