示例#1
0
        public bool Start(bool throwOnError = false)
        {
            try
            {
                if (this.server != null)
                {
                    if (server.Start(throwOnError))
                    {
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                InternalLogger.Log.Error("开启服务端监听出错" + ex.Message);
            }

            return(false);
        }
示例#2
0
        static bool exit             = false; //标记退出while

        static void Main(string[] args)
        {
            tcpServer = new AsyncSocketTcpServer(5003);
            tcpServer.DataReceived  += tcpServer_DataReceived;
            tcpServer.Connected     += tcpServer_Connected;
            tcpServer.DataSent      += tcpServer_DataSent;
            tcpServer.Disconnected  += tcpServer_Disconnected;
            tcpServer.ErrorOccurred += tcpServer_ErrorOccurred;
            tcpServer.Started       += tcpServer_Started;
            tcpServer.Stopped       += tcpServer_Stopped;
            tcpServer.Start(false);

            Console.WriteLine("请在30秒内,开启tcpclient测试端,并进行连接");
            (new Thread(() =>
            {
                Thread.Sleep(30000);
                exit = true;
            })).Start();

            do
            {
                Thread.Sleep(700);
                //如果有客户端连接成功,就向客户端发送一个瓶签号
                if (sessionId != -1)
                {
                    tcpServer.Send(sessionId, Encoding.Default.GetBytes("20181028100111"));
                }
            } while (sessionId == -1 && !exit);

            if (sessionId != -1 || exit)
            {
                if (exit)
                {
                    Console.WriteLine("没有任何客户端进行连接,输入输入任意键,关闭程序");
                }
                if (sessionId != -1)
                {
                    Console.WriteLine("服务端已向客户端发送瓶签号,请验证客户端是否收到。输入任意键,关闭程序");
                }
            }
            Console.ReadLine();
        }