当前位置: 首页>>代码示例>>C#>>正文


C# UdpClient.ReceiveString方法代码示例

本文整理汇总了C#中System.Net.Sockets.UdpClient.ReceiveString方法的典型用法代码示例。如果您正苦于以下问题:C# UdpClient.ReceiveString方法的具体用法?C# UdpClient.ReceiveString怎么用?C# UdpClient.ReceiveString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Sockets.UdpClient的用法示例。


在下文中一共展示了UdpClient.ReceiveString方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Run

        public void Run(params string[] args)
        {
            Console.CancelKeyPress += OnCancelKeyPressed;

            try
            {
                using (var receiver = new UdpClient(8888))
                {
                    using (var sender = new UdpSender(7777))
                    {
                        Console.WriteLine("Welcome to terminal.");
                        Console.WriteLine("Press CTRL+C or enter empty line to stop terminal.");

                        while (_terminalRunning)
                        {
                            while (Console.KeyAvailable)
                            {
                                var line = Console.ReadLine();
                                if (!string.IsNullOrWhiteSpace(line))
                                    sender.Send(line);
                                else
                                    _terminalRunning = false;
                            }

                            while (receiver.Available > 0)
                            {
                                var message = receiver.ReceiveString();
                                if (!string.IsNullOrWhiteSpace(message))
                                    Console.WriteLine($"<- {message}");
                            }

                            Thread.Sleep(1);
                        }

                        Console.WriteLine("Terminal stopped.");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToRecursiveString());
            }

            Console.CancelKeyPress -= OnCancelKeyPressed;
        }
开发者ID:Konard,项目名称:LinksPlatform,代码行数:45,代码来源:TerminalCLI.cs

示例2: Main

        private static void Main()
        {
            Console.CancelKeyPress += OnCancelKeyPressed;

            try
            {
            #if DEBUG
                File.Delete(DefaultDatabaseFilename);
            #endif

                using (var links = new Links(DefaultDatabaseFilename, 8*1024*1024))
                {
                    InitUTF16(links);

                    var sequences = new Sequences(links);

                    PrintContents(links, sequences);

                    Console.WriteLine("Links server started.");
                    Console.WriteLine("Press CTRL+C or ESC to stop server.");

                    using (var sender = new UdpSender(8888))
                    {
                        MessageHandlerCallback handleMessage = message =>
                        {
                            if (!string.IsNullOrWhiteSpace(message))
                            {
                                Console.WriteLine("R.M.: {0}", message);

                                if (message.EndsWith("?"))
                                    sequences.Search(sender, message);
                                else
                                    sequences.Create(sender, message);
                            }
                        };

                        //using (var receiver = new UdpReceiver(7777, handleMessage))
                        using (var receiver = new UdpClient(7777))
                        {
                            while (LinksServerRunning)
                            {
                                while (receiver.Available > 0)
                                    handleMessage(receiver.ReceiveString());

                                while (Console.KeyAvailable)
                                {
                                    var info = Console.ReadKey(true);
                                    if (info.Key == ConsoleKey.Escape)
                                        LinksServerRunning = false;
                                }

                                Thread.Sleep(1);
                            }

                            Console.WriteLine("Links server stopped.");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.WriteToConsole();
            }

            Console.CancelKeyPress -= OnCancelKeyPressed;
        }
开发者ID:happiesanger,项目名称:LinksPlatform,代码行数:66,代码来源:Program.cs


注:本文中的System.Net.Sockets.UdpClient.ReceiveString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。