Exemple #1
0
        static public void RunServer(object obj)
        {
            var peers = new HashSet <Int64>();

            using (var svr = new UDPServer())
            {
                svr.OnConnect = peerId =>
                {
                    peers.Add(peerId);
                    Console.WriteLine("{0} connected", peerId);
                };
                svr.OnReceive = (peerId, bytes, channelId) =>
                {
                    var msg = Encoding.ASCII.GetString(bytes);
                    Console.WriteLine("recv {0} msg {1}", peerId, msg);

                    bytes = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", peerId, msg));
                    foreach (var peer in peers)
                    {
                        if (peer != peerId)
                        {
                            svr.SendBytes(peer, bytes);
                        }
                    }
                };
                svr.OnDisconnect = peerId =>
                {
                    peers.Remove(peerId);
                    Console.WriteLine("{0} disconnected", peerId);
                };

                svr.Listen(1234);

                while (true)
                {
                    svr.Update();
                }
            }
        }
Exemple #2
0
        static void RunUdpServer()
        {
            using (var svr = new UDPServer())
            {
                svr.OnConnect = l =>
                {
                    Console.WriteLine(DateTime.Now.ToLongTimeString() + " connected " + l);
                };
                svr.OnDisconnect = l =>
                {
                    Console.WriteLine(DateTime.Now.ToLongTimeString() + " disconnected " + l);
                };
                svr.OnReceive = (l, bytes, arg3) =>
                {
                    var recved = Encoding.ASCII.GetString(bytes);
                    Console.WriteLine(DateTime.Now.ToLongTimeString() + " receive " + recved);

                    switch (recved)
                    {
                    case "dc":
                        svr.Disconnect(l);
                        break;

                    default:
                        svr.SendBytes(l, bytes, arg3);
                        break;
                    }
                };

                svr.Listen(1234);
                while (true)
                {
                    svr.Update();
                }
            }
        }