| | |
| | | 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;
|
| | | //当网关的连接方式改变时,记录当前的连接方式
|
| | |
| | | /// 启动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地址 |
| | | var netwifi = new Shared.Net.NetWiFi(); |
| | | string phoneIp = netwifi.GetIPAddress(); |
| | | //"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> |
| | |
| | | /// 异步发送数据 |
| | | /// </summary> |
| | | /// <param name="tempPacket"></param> |
| | | public static void BeginSend(System.Net.IPEndPoint iPEndPoint, byte[] bytes) |
| | | public static void BeginSend(IPEndPoint iPEndPoint, byte[] bytes) |
| | | { |
| | | try |
| | | { |
| | | if (udpClient == null)
|
| | | if (Start() == false)
|
| | | {
|
| | | //调试用 |
| | | if (Shared.Phone.HdlUserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1) |
| | | { |
| | | Shared.Phone.HdlLogLogic.Current.WriteLog(3, "udp New不起来"); |
| | | } |
| | | return;
|
| | | }
|
| | | int value = udpClient.Send(bytes, bytes.Length, iPEndPoint);
|