Example #1
0
        private void AcceptCb(IAsyncResult ar)
        {
            try
            {
                Socket targetSocket = Listenfb.EndAccept(ar);
                int    index        = ContributeIndex();
                if (index < 0)
                {
                    targetSocket.Close();
                    Console.WriteLine("[Warning] This server is full.");
                }
                else
                {
                    Connection conn = conns[index];
                    conn.Initialize(targetSocket);
                    Console.WriteLine($"[Info] Server is connected client:{conn.RemoteAddress}.");
                    conn.Socket.BeginReceive(conn.BufferRead, conn.BufferCount, conn.BufferRemain, SocketFlags.None, ReceiveCb, conn);
                }

                Listenfb.BeginAccept(AcceptCb, null);
            }
            catch (Exception ex)
            {
                Console.WriteLine("[Exception] AcceptCb Method is error.");
                Console.WriteLine($"[Error Info] {ex.Message}");
            }
        }
Example #2
0
        public void Startup(string ip, int port)
        {
            IPAddress  ipAd = IPAddress.Parse(ip);
            IPEndPoint ipEp = new IPEndPoint(ipAd, port);

            Listenfb.Bind(ipEp);
            Listenfb.Listen(MaxCapacity);
            Listenfb.BeginAccept(AcceptCb, null);
            Console.WriteLine($"[Startup] {Expression} server.");

            timer.AutoReset = true;
            timer.Enabled   = true;
            timer.Elapsed  += HeartBeatElapsed;
            timer.Start();
        }