Aegis.Network.UDPServer.Bind C# (CSharp) Method

Bind() public method

public Bind ( string ipAddress, int portNo ) : void
ipAddress string
portNo int
return void
        public void Bind(string ipAddress, int portNo)
        {
            lock (this)
            {
                if (_socket != null)
                    throw new AegisException(AegisResult.AlreadyInitialized);

                Array.Clear(_receivedBuffer, 0, _receivedBuffer.Length);

                _endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), portNo);
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                _socket.Bind(_endPoint);
            }

            WaitForReceive();
        }

Usage Example

Beispiel #1
0
        private void Start()
        {
            _packetDispatcher = new MethodSelector<ReceiveData>(this, (ref ReceiveData source, out string key) =>
            {
                key = source.Packet.PacketId.ToString();
                source.Packet.SkipHeader();
            });

            _udp = new UDPServer();
            _udp.EventRead += NetworkEvent_Receive;
            _udp.EventClose += NetworkEvent_Close;
            _udp.Bind("127.0.0.1", 10201);

            _counter = new IntervalCounter(1000);
            _counter.Start();
            (new IntervalTimer(1000, () =>
            {
                DateTime now = DateTime.Now;
                Console.WriteLine(string.Format("[{0}/{1} {2}:{3}:{4}] recv: {5}",
                                        DateTime.Now.Month, DateTime.Now.Day,
                                        DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second,
                                        _counter.Value));
            })).Start();
        }