DeviceIP.cs
5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace Asa.RFID
{
public static class HaoBinIP
{
private static List<HFReaderInfo> info = new List<HFReaderInfo>();
private static UdpClient udp;
private const int PORT = 51060;
/// <summary>
/// 查找IP地址
/// </summary>
/// <param name="localIP"></param>
/// <returns></returns>
public static string[] Find(string localIP)
{
info.Clear();
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 65535);
byte[] arr = Encoding.ASCII.GetBytes("X");
IPEndPoint local = new IPEndPoint(IPAddress.Parse(localIP), PORT);
udp = new UdpClient(local);
udp.Send(arr, arr.Length, ep);
System.Threading.Thread.Sleep(50);
int count = 0;
int time = 0;
System.Threading.Thread tTemp = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ReceiveIP));
tTemp.Start(ep);
while (true)
{
if (info.Count == count)
time += 50;
else
{
time = 0;
count = info.Count;
}
if (time >= 800)
{
tTemp.Abort();
tTemp = null;
break;
}
System.Threading.Thread.Sleep(50);
}
udp.Close();
string[] ip = new string[info.Count];
for (int i = 0; i < ip.Length; i++)
ip[i] = info[i].IP;
return ip;
}
/// <summary>
/// 修改IP地址,需要先查找IP
/// </summary>
/// <param name="localIP">本地IP</param>
/// <param name="beforeIP">修改前的IP</param>
/// <param name="afterIP">修改后的IP</param>
/// <returns></returns>
public static bool Modify(string localIP, string beforeIP, string afterIP)
{
int idx = info.FindIndex(s => s.IP == beforeIP);
if (idx == -1) return false;
IPEndPoint local = new IPEndPoint(IPAddress.Parse(localIP), PORT);
UdpClient udp = new UdpClient(local);
string[] ss = afterIP.Split('.');
string port = "6" + ss[3];
try
{
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(info[idx].IP), 65535);
byte[] buff;
buff = Encoding.ASCII.GetBytes("W" + info[idx].Mac);
udp.Send(buff, buff.Length, ep);
System.Threading.Thread.Sleep(100);
buff = Encoding.ASCII.GetBytes("L");
udp.Send(buff, buff.Length, ep);
System.Threading.Thread.Sleep(50);
buff = Encoding.ASCII.GetBytes("SPN" + port + "|33");
udp.Send(buff, buff.Length, ep);
System.Threading.Thread.Sleep(50);
buff = Encoding.ASCII.GetBytes("SIP" + afterIP + "|34");
udp.Send(buff, buff.Length, ep);
System.Threading.Thread.Sleep(100);
buff = Encoding.ASCII.GetBytes("E|35");
udp.Send(buff, buff.Length, ep);
return true;
}
catch (Exception)
{
return false;
}
finally
{
udp.Close();
}
}
/// <summary>
/// 获取本地IPv4地址
/// </summary>
/// <returns></returns>
public static string[] GetLocal()
{
List<string> str = new List<string>();
IPAddress[] add = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
foreach (IPAddress ip in add)
{
if (ip.AddressFamily.ToString() == "InterNetwork")
str.Add(ip.ToString());
}
return str.ToArray();
}
private static void ReceiveIP(object obj)
{
IPEndPoint ep = (IPEndPoint)obj;
while (true)
{
byte[] buff = udp.Receive(ref ep);
HFReaderInfo ri = new HFReaderInfo { IP = ep.Address.ToString() };
if (buff == null || buff.Length < 20 || buff[0] != 0x41)
continue;
bool cut = true;
for (int i = 0; i < buff.Length; i++)
{
if (buff[i] == 0x2F)
{
if (cut)
{
ri.Mac = Encoding.ASCII.GetString(buff, 1, i - 1);
cut = false;
}
else
{
//info.Port = Convert.ToInt32(Encoding.ASCII.GetString(buff, start, i - start));
break;
}
}
}
info.Add(ri);
System.Threading.Thread.Sleep(5);
}
}
}
internal class HFReaderInfo
{
internal string IP;
internal string Mac;
internal int Port;
internal int Func;
internal byte[] Buffer;
internal byte[] ID;
internal byte ComAddr;
public HFReaderInfo()
{
Func = 0;
Buffer = new byte[28 * 4];
ID = new byte[8];
}
public HFReaderInfo(string ip) : this()
{
IP = ip;
string[] aa = ip.Split('.');
ComAddr = Convert.ToByte(aa[3]);
Port = 6000 + Convert.ToInt32(aa[3]);
}
}
}