Example #1
0
        private static void UdpDemo()
        {
            int port = 12345;
            int svc_c = 0, cli_c = 0, cli_c2 = 0;
            var serverProvider = NetTool.CreateUdpSever(4096, 32);

            serverProvider.ReceivedOffsetHanlder = new OnReceivedSegmentHandler((SegmentToken session) =>
            {
                ++svc_c;

                Log.L("from client:" + Encoding.UTF8.GetString(session.Data.buffer, session.Data.offset, session.Data.size));
                serverProvider.Send(new SegmentOffset(Encoding.UTF8.GetBytes("i'm server" + DateTime.Now)), session.sToken.TokenIpEndPoint);
            });
            serverProvider.Start(port);
            var clientProvider = NetTool.CreateUdpClient(4096, 4);

            clientProvider.ReceivedOffsetHandler = new OnReceivedSegmentHandler((SegmentToken session) =>
            {
                Log.L("from server :" + Encoding.UTF8.GetString(session.Data.buffer, session.Data.offset,
                                                                session.Data.size));

                ++cli_c;
            });
            bool isConn = clientProvider.Connect(port, "127.0.0.1");

            int c = 10;

            while (c > 0)
            {
                //string msg = Console.ReadLine();
                //if (msg == "exit")
                //    break;

                clientProvider.Send(new SegmentOffset(Encoding.UTF8.GetBytes((--c).ToString())));
            }
            Log.L(string.Format("完成svc:{0};cli1:{1};cli2:{2}", svc_c, cli_c, cli_c2));

            Console.ReadKey();
            serverProvider.Dispose();
            clientProvider.Dispose();
        }