| | |
| | | { |
| | | try |
| | | { |
| | | if (FindGateWaySocket.busSocket == null || FindGateWaySocket.busSocket.Available <= 0) |
| | | if (FindGateWaySocket.udpClient == null || FindGateWaySocket.udpClient.Available <= 0) |
| | | { |
| | | System.Threading.Thread.Sleep(500); |
| | | continue; |
| | | } |
| | | var bytes = new byte[1024]; |
| | | var len = FindGateWaySocket.busSocket.Receive(bytes, bytes.Length, System.Net.Sockets.SocketFlags.None); |
| | | }
|
| | | var ipEndPoint = new System.Net.IPEndPoint(0, 0); |
| | | var bytes = FindGateWaySocket.udpClient.Receive(ref ipEndPoint); |
| | | if (bytes[43] == 0xA2) |
| | | { |
| | | //广播回复网关的基本信息处理 |
| | |
| | | } |
| | | } |
| | | } |
| | | catch { } |
| | | catch (Exception ex) |
| | | {
|
| | | //调试用
|
| | | if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
|
| | | {
|
| | | Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "Receive异常\r\n" + ex.Message);
|
| | | } |
| | | } |
| | | } |
| | | }) |
| | | { IsBackground = true }.Start(); |
| | |
| | | public static class FindGateWaySocket |
| | | { |
| | | //本地Socket |
| | | public static Socket busSocket; |
| | | public static UdpClient udpClient; |
| | | |
| | | /// <summary> |
| | | /// 启动Socket接收和发送功能 |
| | | /// </summary> |
| | | /// <param name="port"></param> |
| | | public static void Start(int port = 7624) |
| | | public static void Start() |
| | | { |
| | | if (IsRunning) |
| | | if (udpClient!=null) |
| | | { |
| | | return; |
| | | } |
| | | //定义网络类型,数据连接类型和网络协议UDP |
| | | busSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); |
| | | busSocket.EnableBroadcast = true; |
| | | busSocket.ReceiveTimeout = 1000; |
| | | busSocket.SendTimeout = 1000; |
| | | busSocket.Bind(new IPEndPoint(IPAddress.Any, port)); |
| | | udpClient = new UdpClient { EnableBroadcast = true }; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | { |
| | | try |
| | | { |
| | | busSocket?.Close(); |
| | | udpClient?.Close(); |
| | | } |
| | | catch { } |
| | | busSocket = null; |
| | | udpClient = null; |
| | | |
| | | Console.WriteLine("BusSocket关闭成功!"); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 当前的Socket是否运行 |
| | | /// </summary> |
| | | public static bool IsRunning |
| | | { |
| | | get |
| | | { |
| | | return busSocket == null ? false : true; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | { |
| | | try |
| | | { |
| | | if (IsRunning)
|
| | | if (udpClient == null)
|
| | | {
|
| | | busSocket.BeginSendTo(bytes, 0, bytes.Length, SocketFlags.None, iPEndPoint, new AsyncCallback(asyncEndSend), null);
|
| | | return;
|
| | | }
|
| | |
|
| | | udpClient.Send(bytes, bytes.Length, iPEndPoint);
|
| | | } |
| | | catch (Exception ex) |
| | | {
|
| | |
| | | }
|
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 异步发送数据结束 |
| | | /// </summary> |
| | | /// <param name="iar"></param> |
| | | private static void asyncEndSend(IAsyncResult iar) |
| | | { |
| | | try |
| | | { |
| | | int bytesSent = busSocket.EndSendTo(iar); |
| | | } |
| | | catch (Exception ex)
|
| | | {
|
| | | //调试用
|
| | | if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
|
| | | {
|
| | | Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "异步发送结束异常(asyncEndSend)\r\n" + ex.Message);
|
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | #endregion |
| | | } |
| | | } |
| | | } |