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(); }}
阅读全文
0 0
- udp广播测试代码
- UDP广播的代码
- java udp广播代码示例
- udp 局域网广播发送代码
- udp 局域网广播接受代码
- UDP广播测试端口复用 SO_REUSEADDR
- Network:UDP广播消息的测试
- 正确使用UDP广播(C++代码)
- 正确使用UDP广播(C++代码)
- 正确使用UDP广播(C++代码)
- udp 广播 和 多播 java 代码
- UDP广播
- UDP广播
- udp广播
- UDP广播
- UDP广播
- UDP广播
- UDP 广播
- arp欺骗
- PullToRefreshListView上拉刷新,下拉加载
- 人机智能交互技术示例-Leap Motion通过ROS控制机械手Gazebo仿真
- 915总结
- Docker CE安装及配置国内镜像加速教程
- udp广播测试代码
- ubuntu 16.04 安装Xilinx Platform Cable USB 驱动
- Hdu 6203 ping ping ping dfs序+树状数组维护
- mysql总结(二)
- [Leetcode] #121#122#123#188 Best Time to Buy and Sell Stock I & II & III & IV
- jsp 中${ } 是什么意思? <c:forEach var="mem" items="${memberList}">中的c:forEach 是什么写法,什么意
- Python黑魔法 --- 异步IO( asyncio) 协程
- 创建线程后为什么马上调用CloseHandle()来关闭句柄
- 购物车,TextView右上角实现红色数字变化的实现