Пример #1
0
        public async Task StartUdp()
        {
            try
            {
                #if DEBUG
                Console.WriteLine("Start Server .....");
                #endif
                //   IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress  ipAddress     = IPAddress.Any;
                IPEndPoint localEndPoint = new IPEndPoint(ipAddress, UDPPort);

                var listener = new Socket
                               (
                    ipAddress.AddressFamily
                    ,
                    SocketType.Dgram
                    ,
                    ProtocolType.Udp
                               );

                listener.Bind(localEndPoint);


                Task.Run(() =>
                {
                    agen:
                    StateObjectupb state = new StateObjectupb();
                    state.workSocket     = listener;
                    state.StopAndLoopReading.Reset();
                    IPEndPoint sender     = new IPEndPoint(IPAddress.Any, 0);
                    EndPoint tempRemoteEP = (EndPoint)sender;
                    if (SocketConnected(state.workSocket))
                    {
                        listener.BeginReceiveFrom(state.buffer, 0, state.BufferSize, 0, ref tempRemoteEP, new AsyncCallback(ReadCallbackudp), state);
                    }

                    state.StopAndLoopReading.WaitOne();

                    goto agen;
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Пример #2
0
 public virtual bool WatchUdpByPort(ref StateObjectupb StateObjectupb, out int portOut, in int portIn = 0)
Пример #3
0
        public static async void startRciverUDP()
        {
            try

            {
                IPHostEntry ipHostInfo = Dns.GetHostEntry(ip);//Dns.GetHostEntry("net.super-bots.com");
                IPAddress   ipAddress  = ipHostInfo.AddressList[ipHostInfo.AddressList.Length - 1];
                IPEndPoint  remoteEP   = new IPEndPoint(ipAddress, portudp);

                clientudp      = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                clientreserudp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);


                List <Data> datalist = null;

                sendDone.Reset();
                datalist = new List <Data>();

                clientudp.Connect(remoteEP);
                clientreserudp.Bind(clientudp.LocalEndPoint);
                CancellationToken ct = startRciverUDPStop.Token;
                await Task.Run(() =>
                {
                    StateObjectupb state = new StateObjectupb();
                    state.workSocket     = clientreserudp;
                    ct.ThrowIfCancellationRequested();

                    while (true)
                    {
                        if (ct.IsCancellationRequested)
                        {
                            ct.ThrowIfCancellationRequested();
                        }
                        receiveDone.Reset();
                        Receiveudp(ref state);
                        receiveDone.WaitOne();
                        if (state.returnObjectFromEventPresor != null)
                        {
                            Console.WriteLine("Response received udp : {0}", state.returnObjectFromEventPresor.ToString());
                        }
                        state.returnObjectFromEventPresor = null;
                    }
                }, startRciverUDPStop.Token);
            }
            catch (OperationCanceledException e)
            {
                Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                startRciverUDPStop.Dispose();
                startRciverUDPStop = new CancellationTokenSource();
                startRciverUDP();
                startSendUDP(new PackSendData(0, new List <Data>()
                {
                    new Data((byte)0, 0)
                }));
            }
        }