Example #1
0
        static void Test()
        {

            TcpClientHost Client;
            //终端服务器连接
            var serverSession = new ServerSession();
            TcpClientLog _log = new TcpClientLog();
            Client = new TcpClientHost(new TcpClientHostConfig("127.0.0.1", 8181)
                , serverSession
                , _log);
            Client.ConnectionToServerSuccessAction = x =>
            {
                if (!serverSession.Accredit(Client.ClientToken))
                {
                    _log.LogInfo(string.Format("未通过服务器端授权\r\n{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                }
                else
                {
                    Client.StartReceive();
                    _log.LogInfo(string.Format("通过服务器端授权\r\n{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                }
            };

            Client.SocketErrorAction = (ex) =>
            {

            };

            Client.ConnectionToServerSuccessAction = (T) =>
            {
                Action action = () =>
                    {
                        while (true)
                        {
                            T.Send(Guid.NewGuid().ToString());
                            Thread.Sleep(200);
                        }
                    };

                Thread thread = new Thread(new ThreadStart(action));
                thread.IsBackground = true;
                thread.Start();

                _log.LogInfo(Client.ClientToken.TcpClient.Client.LocalEndPoint.ToString());

            };

            Client.ConnectionToServer();
        }
Example #2
0
        static void Test()
        {

            int ipPoint = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["ipPoint"]);
            string ipBind = System.Configuration.ConfigurationManager.AppSettings["ipBind"];

            TcpClientHost Client;
            //终端服务器连接
            var serverSession = new ServerSession();

            Client = new TcpClientHost(new TcpClientConfig(ipBind, ipPoint)
                , serverSession
                , TcpClientLog.GetInstance);
            Client.ConnectionToServerSuccessAction = x =>
            {
                serverSession.Accredit(Client.ClientToken, (result) =>
                {
                    if (result)
                    {
                        TcpClientLog.GetInstance.LogInfo(string.Format("未通过服务器端授权\r\n{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                    }
                    else
                    {
                        TcpClientLog.GetInstance.LogInfo(string.Format("通过服务器端授权\r\n{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                    }
                });
            };

            Client.SocketErrorAction = (ex) =>
            {
                TcpClientLog.GetInstance.LogError(ex.Message, ex, "socket.txt");
            };



            Client.ConnectionToServerSuccessAction = (T) =>
            {
                Action action = () =>
                {
                    while (true)
                    {
                        try
                        {
                            int seed = 50000;
                            Task[] tasks = new Task[seed];

                            Stopwatch sw = new Stopwatch();
                            sw.Restart();
                            for (var i = 0; i < seed; i++)
                            {
                                tasks[i] = T.SendAsync("{" + 1 + "}");
                                Thread.SpinWait(1);
                            }

                            Task.WaitAll(tasks);
                            sw.Stop();
                            Console.WriteLine("Count:{0},Milliseconds:{1}", seed, sw.ElapsedMilliseconds);
                            //T.SendAsync("{" + Guid.NewGuid().ToString() + DateTime.Now.ToShortDateString() + "}").Wait();
                        }
                        catch (Exception ex)
                        {

                        }

                        Thread.Sleep(5000);
                    }
                };

                Thread thread = new Thread(new ThreadStart(action));
                thread.IsBackground = true;
                thread.Start();

                TcpClientLog.GetInstance.LogInfo(Client.ClientToken.TcpClient.Client.LocalEndPoint.ToString());

            };

            Client.ConnectionToServer();
        }