示例#1
0
        public static UdpUser ConnectTo(string hostname, int port)
        {
            var connection = new UdpUser();

            connection.Client.Connect(hostname, port);
            return(connection);
        }
示例#2
0
        private static void Main(string[] args)
        {
            //create a new server
            var server = new UdpListener();

            //start listening for messages and copy the messages back to the client
            Task.Factory.StartNew(async() => {
                while (true)
                {
                    Received received = await server.Receive();
                    server.Reply("copy " + received.Message, received.Sender);
                    if (received.Message == "quit")
                    {
                        break;
                    }
                }
            });

            //create a new client
            UdpUser client = UdpUser.ConnectTo("127.0.0.1", 9989);

            //wait for reply messages from server and send them to console
            Task.Factory.StartNew(async() => {
                while (true)
                {
                    try
                    {
                        Received received = await client.Receive();
                        Console.WriteLine(received.Message);
                        if (received.Message.Contains("quit"))
                        {
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.Write(ex);
                    }
                }
            });

            //type ahead :-)
            string read;

            do
            {
                read = Console.ReadLine();
                client.Send(read);
            } while (read != "quit");
        }