提交 | 用户 | 时间
ce910c 1 package com.netsdk.demo.module;
H 2
3 import java.net.Inet4Address;
4 import java.net.InetAddress;
5 import java.net.NetworkInterface;
6 import java.net.SocketException;
7 import java.util.ArrayList;
8 import java.util.Enumeration;
9 import java.util.List;
10
11 import com.sun.jna.Memory;
12 import com.sun.jna.Pointer;
13
14 import com.netsdk.lib.NetSDKLib.*;
15 import com.netsdk.lib.ToolKits;
16 import com.netsdk.lib.enumeration.EM_SEND_SEARCH_TYPE;
17 import com.netsdk.lib.structure.NET_IN_STARTSERACH_DEVICE;
18 import com.netsdk.lib.structure.NET_OUT_STARTSERACH_DEVICE;
19
20
21 /**
22  * 设备搜索接口实现
23  * 主要功能有 : 设备组播和广播搜索、设备IP单播搜索
24  */
25 public class DeviceSearchModule {
26
27     /**
28      * 设备组播和广播搜索
29      * @throws SocketException 
30      */
31     public static LLong multiBroadcastDeviceSearch(fSearchDevicesCBEx cbSearchDevices,String szlocalIp) throws SocketException {
32         NET_IN_STARTSERACH_DEVICE pInparm = new NET_IN_STARTSERACH_DEVICE();
33         
34         pInparm.cbSearchDevices=cbSearchDevices;        
35         System.arraycopy(szlocalIp.getBytes(), 0, pInparm.szLocalIp, 0, szlocalIp.getBytes().length);
36         pInparm.emSendType=EM_SEND_SEARCH_TYPE.EM_SEND_SEARCH_TYPE_MULTICAST_AND_BROADCAST.ordinal();
37         
38         Pointer pInBuf =new Memory(pInparm.size());
39         ToolKits.SetStructDataToPointer(pInparm, pInBuf, 0);
40         
41         NET_OUT_STARTSERACH_DEVICE pOutparm =new NET_OUT_STARTSERACH_DEVICE();
42         
43         Pointer pOutBuf =new Memory(pOutparm.size());
44         ToolKits.SetStructDataToPointer(pOutparm, pOutBuf, 0);
45         
46         return LoginModule.netsdk.CLIENT_StartSearchDevicesEx(pInBuf, pOutBuf);
47     }
48     
49     /**
50      * 停止设备组播和广播搜索
51      */
52     public static void stopDeviceSearch(LLong m_DeviceSearchHandle) {
53         if(m_DeviceSearchHandle.longValue() == 0) {
54             return;
55         }
56         
57         LoginModule.netsdk.CLIENT_StopSearchDevices(m_DeviceSearchHandle);
58         m_DeviceSearchHandle.setValue(0);
59     }
60     
61     /**
62      * 设备IP单播搜索
63      * @param startIP 起始IP
64      * @param nIpNum IP个数,最大 256
65      * @throws SocketException 
66      */
67     public static boolean unicastDeviceSearch(String localIp,String startIP, int nIpNum, fSearchDevicesCB cbSearchDevices) throws SocketException {
68         String[] szIPStr = startIP.split("\\.");
69         
70         DEVICE_IP_SEARCH_INFO deviceSearchInfo = new DEVICE_IP_SEARCH_INFO();
71         deviceSearchInfo.nIpNum = nIpNum;
72         for(int i = 0; i < deviceSearchInfo.nIpNum; i++) {
73             System.arraycopy(getIp(szIPStr, i).getBytes(), 0, deviceSearchInfo.szIPArr[i].szIP, 0, getIp(szIPStr, i).getBytes().length);
74         }
75         if(LoginModule.netsdk.CLIENT_SearchDevicesByIPs(deviceSearchInfo, cbSearchDevices, null, localIp, 6000)) {
76             System.out.println("SearchDevicesByIPs Succeed!");
77             return true;
78         }
79         return false;
80     }
81     
82     public static String getIp(String[] ip, int num) {
83         String szIp = "";
84         if(Integer.parseInt(ip[3]) >= 255) {
85             szIp = ip[0] + "." + ip[1] + "." + String.valueOf(Integer.parseInt(ip[2]) + 1) + "." + String.valueOf(Integer.parseInt(ip[3]) + num - 255);
86         } else {
87             szIp = ip[0] + "." + ip[1] + "." + ip[2] + "." + String.valueOf(Integer.parseInt(ip[3]) + num);
88         }
89         
90         return szIp;
91     }
92     
93     /**
94      * 获取多网卡IP 
95      */
96     public static List<String> getHostAddress() throws SocketException {
97         
98         List<String> ipList = new ArrayList<String>();
99         if(NetworkInterface.getNetworkInterfaces() == null) {
100             return ipList;
101         }        
102         Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();       
103         
104         while (networkInterfaces.hasMoreElements()) {
105             NetworkInterface networkInterface = networkInterfaces.nextElement();
106             Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
107             
108             while (inetAddresses.hasMoreElements()) {
109                 InetAddress inetAddress = inetAddresses.nextElement();                             
110                 
111                 if (inetAddress.isLoopbackAddress()) {//回路地址,如127.0.0.1
112 //                    System.out.println("loop addr:" + inetAddress);
113                 } else if (inetAddress.isLinkLocalAddress()) {//169.254.x.x
114 //                    System.out.println("link addr:" + inetAddress);
115                 } else if(inetAddress instanceof Inet4Address){
116                     //非链接和回路真实ip                                          
117                          String localname = inetAddress.getHostName();
118                          String localip = inetAddress.getHostAddress();
119                          ipList.add(localip);                                                                              
120                 }
121             }
122         }        
123         return ipList;
124     }
125 }