Ejemplo n.º 1
0
        //接收数据
        private void OnRecievedData(IAsyncResult ar)
        {
            UdpClient  udpclient  = (System.Net.Sockets.UdpClient)ar.AsyncState;
            IPEndPoint ipendpoint = null;

            try
            {
                byte[] receiveBytes = udpclient.EndReceive(ar, ref ipendpoint);
                udpclient.BeginReceive(new AsyncCallback(OnRecievedData), udpclient);


                if (this.OnReceivedData != null)
                {
                    this.OnReceivedData(this, new ReceivedDataEventArgs(ipendpoint, receiveBytes));
                }
            }
            catch (Exception ex)
            {
                IsOpen = false;
                try
                {
                    UdpBussiness.DelSocket(this, ipendpoint);

                    UdpBussiness.UdpDisconnected(this, ipendpoint);

                    UDPClient.Client.Dispose();
                    Start();
                }
                catch (Exception e)
                { Service.ServiceControl.log.Error(DateTime.Now + e.ToString()); }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 从列表删除Udp对象
        /// </summary>
        /// <param name="US">udp服务</param>
        /// <param name="Second">秒</param>
        public static void DelClosSocket(UdpServer US, int Second)
        {
            string           ServiceID = US.ServiceID;
            List <UdpSocket> Us        = US.Us;
            var       temp             = from u in Us where u.CONNECTTIME != null select u;
            ArrayList al = new ArrayList();

            foreach (var item in temp)
            {
                DateTime dt1 = item.DATATIME.Value.AddSeconds(Second);
                DateTime dt2 = DateTime.Now;
                if (DateTime.Compare(dt1, dt2) < 0)
                {
                    al.Add(item);
                }
            }
            foreach (UdpSocket item in al)
            {
                UdpBussiness.UdpDisconnected(US, item.IpEndPoint);
                item.IpEndPoint  = null;
                item.CONNECTTIME = null;
                item.DATATIME    = null;
            }
            al = null;
        }
Ejemplo n.º 3
0
 //管理socket的一系列操作
 void SocketManager(object sender)
 {
     try
     {
         UdpBussiness.DelClosSocket(udp, 60);
     }
     catch (Exception ex)
     { Service.ServiceControl.log.Error(DateTime.Now + ex.ToString()); }
 }
Ejemplo n.º 4
0
 //管理socket的一系列操作
 void SocketManager(object sender)
 {
     try
     {
         UdpBussiness.DelClosSocket(udp, 60);
     }
     catch (Exception ex)
     { }
 }
Ejemplo n.º 5
0
 //回复数据的方法
 void SendData(object sender)
 {
     try
     {
         //UdpBussiness.SendData(udp);
         UdpBussiness.SendCommand(udp);
     }
     catch (Exception ex)
     {
     }
 }
Ejemplo n.º 6
0
 //回复数据的方法
 void SendData()
 {
     while (true)
     {
         try
         {
             UdpBussiness.SendCommand(udp);
         }
         catch (Exception ex)
         {
             Service.ServiceControl.log.Error(DateTime.Now + ex.ToString());
         }
         Thread.Sleep(500);
     }
 }
Ejemplo n.º 7
0
        public void Start()
        {
            Us_Init();
            if (!IsOpen)
            {
                //(new Service.ServiceControl()).ToQxsd(Service.ServiceEnum.NFOINDEX.UDP);
                UDPClient = new UdpClient(new IPEndPoint(IPAddress.Parse(IP), PORT));
                UDPClient.BeginReceive(new AsyncCallback(OnRecievedData), UDPClient);
                #region 改代码段设置解决了UDP通讯的10054异常
                uint IOC_IN            = 0x80000000;
                uint IOC_VENDOR        = 0x18000000;
                uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
                UDPClient.Client.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);
                #endregion
                IsOpen = true;

                UdpBussiness.ToQusd();
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 从列表删除Udp对象
        /// </summary>
        /// <param name="US">udp服务</param>
        /// <param name="Minute">分钟</param>
        public static void DelClosSocket(UdpServer US, int Minute)
        {
            string           ServiceID = US.ServiceID;
            List <UdpSocket> Us        = US.Us;

            lock (Us)
            {
                ArrayList al = new ArrayList();
                foreach (UdpSocket item in Us)
                {
                    if (item.DATATIME.ToString("yyyy-MM-dd HH:mm:ss") != "0001-01-01 00:00:00")
                    {
                        DateTime dt1 = item.DATATIME.AddSeconds(Minute);
                        DateTime dt2 = DateTime.Now;
                        if (DateTime.Compare(dt1, dt2) < 0)
                        {
                            al.Add(item);
                        }
                    }
                    else
                    {
                        DateTime dt1 = item.CONNECTTIME.AddSeconds(Minute);
                        DateTime dt2 = DateTime.Now;
                        if (DateTime.Compare(dt1, dt2) < 0)
                        {
                            al.Add(item);
                        }
                    }
                }

                foreach (UdpSocket item in al)
                {
                    UdpBussiness.UdpDisconnected(US, item.IpEndPoint);

                    Us.Remove(item);
                }
                al = null;
            }
        }