示例#1
0
        public bool Connect(IPEndPoint SendingEndPoint, IPEndPoint ReceivingEndPoint)
        {
            if (ReceivingClosed)
            {
                return(false);
            }

            if (udpSendingClient != null)
            {
                Disconnect();
            }

            try
            {
                this.SendingEndPoint   = SendingEndPoint;
                udpSendingClient       = new UdpClient();
                this.ReceivingEndPoint = ReceivingEndPoint;
                udpReceivingClient     = new UdpClient(ReceivingEndPoint);
            } catch (Exception) {
                return(false);
            }

            if (ReceivingActive)
            {
                ResumeReceiving.Set();
            }
            else
            {
                ReceivingActive = true;
                ReceivingThread.Start();
            }
            return(true);
        }
示例#2
0
        private void ReceiveMessages()
        {
            while (ReceivingActive)
            {
                try
                {
                    /* udpSendingClient is a flag of communication enabled */
                    if (udpSendingClient != null)
                    {
                        IPEndPoint tempReceivingEndPoint  = this.ReceivingEndPoint;
                        UdpClient  tempUdpReceivingClient = this.udpReceivingClient;
                        //udpReceivingClient = new UdpClient(tempReceivingEndPoint);
                        if (tempUdpReceivingClient != null && tempReceivingEndPoint != null)
                        {
                            try
                            {
                                byte[] udpDatagram = udpReceivingClient.Receive(ref tempReceivingEndPoint);
                                if (udpReceivingHandler != null)
                                {
                                    udpReceivingHandler(this, udpDatagram);
                                }
                            }
                            catch (Exception)
                            {
                            }
                        }

                        /*finally
                         * {
                         *  udpReceivingClient.Close();
                         * }*/
                    }
                    else
                    {
                        ResumeReceiving.WaitOne();
                    }
                }
                catch (Exception)
                {
                }
            }
        }