| | |
| | | new System.Threading.Thread(() => |
| | | { |
| | | var broadBytes = new byte[44]; |
| | | var waitCount = 0; |
| | | |
| | | while (true) |
| | | { |
| | |
| | | |
| | | if (listReceiveIP.Count > 0)
|
| | | {
|
| | | waitCount = 0;
|
| | | //局域网能够广播得到网关
|
| | | ZbGateway.IsRemote = false;
|
| | | //当网关的连接方式改变时,记录当前的连接方式
|
| | |
| | | }
|
| | | else
|
| | | {
|
| | | waitCount++;
|
| | | if (waitCount >= 2)
|
| | | {
|
| | | //两次都搜不到,才标记为远程
|
| | | continue;
|
| | | }
|
| | | waitCount = 0;
|
| | | //局域网广播不到网关
|
| | | ZbGateway.IsRemote = true;
|
| | | //当网关的连接方式改变时,记录当前的连接方式
|
| | |
| | | }
|
| | | var ipEndPoint = new System.Net.IPEndPoint(IPAddress.Any, 0); |
| | | var bytes = FindGateWaySocket.udpClient.Receive(ref ipEndPoint); |
| | | if (bytes[43] == 0xA2) |
| | | if (bytes.Length > 43 && bytes[43] == 0xA2) |
| | | { |
| | | //广播回复网关的基本信息处理 |
| | | var ipAddress = $"{bytes[3]}.{bytes[4]}.{bytes[5]}.{bytes[6]}"; |
| | |
| | | /// 启动Socket接收和发送功能 |
| | | /// </summary> |
| | | /// <param name="port"></param> |
| | | public static void Start() |
| | | public static bool Start() |
| | | { |
| | | if (udpClient!=null) |
| | | if (udpClient != null) |
| | | { |
| | | return; |
| | | return true; |
| | | } |
| | | udpClient = new UdpClient { EnableBroadcast = true }; |
| | | |
| | | //获取手机的wifi地址 |
| | | #if Android |
| | | var netwifi = new Shared.Net.NetWiFi(); |
| | | string phoneIp = netwifi.GetIPAddress(); |
| | | #endif |
| | | #if iOS |
| | | string phoneIp = Shared.Net.NetWiFi.GetIPAddress(); |
| | | #endif |
| | | //"127.0.0.1"是底层库里面设置的初始默认值 |
| | | if (phoneIp != "127.0.0.1") |
| | | { |
| | | udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse(phoneIp), 7624)); |
| | | udpClient.EnableBroadcast = true; |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | { |
| | | try |
| | | { |
| | | if (udpClient == null)
|
| | | if (Start() == false)
|
| | | {
|
| | | //调试用 |
| | | if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1) |
| | | { |
| | | Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "udp New不起来"); |
| | | } |
| | | return;
|
| | | }
|
| | | int value = udpClient.Send(bytes, bytes.Length, iPEndPoint);
|