Beispiel #1
0
        static void StartMulticastAcceptor()
        {
            IPAddress             localIPAddr = IPAddress.Any;
            AsyncDatagramAcceptor acceptor    = new AsyncDatagramAcceptor();

            acceptor.FilterChain.AddLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Encoding.UTF8)));

            // Define a MulticastOption object specifying the multicast group
            // address and the local IPAddress.
            // The multicast group address is the same as the address used by the client.
            MulticastOption mcastOption = new MulticastOption(mcastAddress, localIPAddr);

            acceptor.SessionConfig.MulticastOption = mcastOption;

            acceptor.SessionOpened += (s, e) =>
            {
                Console.WriteLine("Opened: {0}", e.Session.RemoteEndPoint);
            };
            acceptor.MessageReceived += (s, e) =>
            {
                Console.WriteLine("Received from {0}: {1}", e.Session.RemoteEndPoint, e.Message);
            };

            acceptor.Bind(new IPEndPoint(localIPAddr, mcastPort));

            Console.WriteLine("Acceptor: current multicast group is: " + mcastOption.Group);
            Console.WriteLine("Acceptor: current multicast local address is: " + mcastOption.LocalAddress);
            Console.WriteLine("Waiting for multicast packets.......");
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            //tcp
//            var acceptor = new AsyncSocketAcceptor();
//            acceptor.FilterChain.AddLast("logger", new LoggingFilter());

            //udp
            var acceptor = new AsyncDatagramAcceptor();

            acceptor.SessionConfig.ReuseAddress = true;


            acceptor.Activated       += (s, e) => Console.WriteLine("ACTIVATED");
            acceptor.Deactivated     += (s, e) => Console.WriteLine("DEACTIVATED");
            acceptor.SessionCreated  += (s, e) => e.Session.Config.SetIdleTime(IdleStatus.BothIdle, 10);
            acceptor.SessionOpened   += (s, e) => Console.WriteLine("OPENED");
            acceptor.SessionClosed   += (s, e) => Console.WriteLine("CLOSED");
            acceptor.SessionIdle     += (s, e) => Console.WriteLine("*** IDLE #" + e.Session.GetIdleCount(IdleStatus.BothIdle) + " ***");
            acceptor.ExceptionCaught += (s, e) =>
            {
                Console.WriteLine(e.Exception);
                e.Session.Close(true);
            };
            acceptor.MessageReceived += (s, e) =>
            {
                var income = (IoBuffer)e.Message;
//                var resvData = income.GetInt64();
//                Console.WriteLine("Received : " + resvData);
//                var outcome = IoBuffer.Allocate(income.Capacity);
//                outcome.PutInt64(resvData);

                var msg = income.Duplicate().GetString(Encoding.UTF8);
                Console.WriteLine("Received : " + msg);

                var outcome = IoBuffer.Allocate(income.Capacity);
                outcome.Put(income);
                outcome.Flip();
                e.Session.Write(outcome);
            };

            acceptor.Bind(new IPEndPoint(IPAddress.Any, Port));
            Console.WriteLine("UDPServer listening on port " + Port);
            Console.ReadLine();
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            AsyncDatagramAcceptor acceptor = new AsyncDatagramAcceptor();

            acceptor.FilterChain.AddLast("logger", new LoggingFilter());
            acceptor.SessionConfig.ReuseAddress = true;

            acceptor.ExceptionCaught += (s, e) =>
            {
                Console.WriteLine(e.Exception);
                e.Session.Close(true);
            };
            acceptor.MessageReceived += (s, e) =>
            {
                IoBuffer buf = e.Message as IoBuffer;
                if (buf != null)
                {
                    Console.WriteLine("New value for {0}: {1}", e.Session.RemoteEndPoint, buf.GetInt64());
                }
            };
            acceptor.SessionCreated += (s, e) =>
            {
                Console.WriteLine("Session created...");
            };
            acceptor.SessionOpened += (s, e) =>
            {
                Console.WriteLine("Session opened...");
            };
            acceptor.SessionClosed += (s, e) =>
            {
                Console.WriteLine("Session closed...");
            };
            acceptor.SessionIdle += (s, e) =>
            {
                Console.WriteLine("Session idle...");
            };

            acceptor.Bind(new IPEndPoint(IPAddress.Any, port));
            Console.WriteLine("UDPServer listening on port " + port);
            Console.ReadLine();
        }