예제 #1
0
        static async Task Main(string[] args)
        {
            // Create a task that finishes when Ctrl+C is pressed
            var taskCompletionSource = new TaskCompletionSource <bool>();

            // Capture the Control + C event
            Console.CancelKeyPress += (s, e) =>
            {
                Console.WriteLine("Exiting");
                taskCompletionSource.SetResult(true);

                // Prevent the Main task from being destroyed prematurely.
                e.Cancel = true;
            };

            Console.WriteLine("Press <Ctrl>+C to exit");

            // Create a resource handler.
            var myHandler = new CoapResourceHandler();

            // Register a /hello resource.
            myHandler.Resources.Add(new HelloResource("/hello"));

            // Create a new CoapServer using UDP as it's base transport
            var myServer = new CoapServer(new CoapUdpTransportFactory());

            try
            {
                // Listen to all ip address and subscribe to multicast requests.
                await myServer.BindTo(new CoapUdpEndPoint(Coap.Port) { JoinMulticast = true });

                // Start our server.
                await myServer.StartAsync(myHandler, CancellationToken.None);

                Console.WriteLine("Server Started!");

                // Wait indefinitely until the application quits.
                await taskCompletionSource.Task;
            }
            catch (Exception ex)
            {
                // Canceled tasks are expected, safe to ignore.
                if (ex is TaskCanceledException)
                {
                    return;
                }

                Console.WriteLine($"Exception caught: {ex}");

                Console.WriteLine($"Press <Enter> to exit");
                Console.Read();
            }
            finally
            {
                Console.WriteLine("Shutting Down Server");
                await myServer.StopAsync(CancellationToken.None);
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            var failed = false;

            var myHandler = new CoapResourceHandler();

            myHandler.Resources.Add(new HelloResource("/hello"));

            var myServer = new CoapServer(new CoapUdpTransportFactory());

            try
            {
                myServer.BindTo(new CoapUdpEndPoint(IPAddress.Any, Coap.Port)
                {
                    JoinMulticast = true
                });

                myServer.StartAsync(myHandler, CancellationToken.None).GetAwaiter().GetResult();

                Console.WriteLine("Server Started!");

                Console.WriteLine("Press <Enter> to exit");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                failed = true;
                Console.WriteLine($"{ex.GetType().Name} occured: {ex.Message}");
                Console.WriteLine(ex.StackTrace);
            }
            finally
            {
                Console.WriteLine("Shutting Down Server");
                myServer.StopAsync(CancellationToken.None).GetAwaiter().GetResult();
            }

            if (failed)
            {
                Console.ReadLine();
            }
        }