Beispiel #1
0
        private void on_accept(IAsyncResult result)
        {
            ListenParas paras = (ListenParas)result.AsyncState;

            if (paras.ServerSocket == null)
            {
                return;
            }

            Socket client_socket = null;

            try
            {
                client_socket = paras.ServerSocket.EndAccept(result);
            }
            catch (System.Exception ex)
            {
                Log.ErrorAf("[Net] Host={0} Port={1} EndAccept Error {2}", paras.Host, paras.Port, ex.ToString());
                return;
            }

            process_accept(client_socket, paras.Factory);

            Log.InfoAf("[Net] Host={0} Port={1} Accept Connection Success", paras.Host, paras.Port);
            try
            {
                paras.ServerSocket.BeginAccept(on_accept, paras);
            }
            catch (System.Exception ex)
            {
                Log.ErrorAf("[Net] Host={0} Port={1} BeginAccept Next Error {2}", paras.Host, paras.Port, ex.ToString());
                return;
            }
        }
Beispiel #2
0
        public bool Listen(string host, UInt32 port, ISessionfactory factory, int listen_max_count)
        {
            if (factory == null)
            {
                return(false);
            }

            if (listen_max_count == 0)
            {
                listen_max_count = NetDef.LISTEN_MAX_COUNT;
            }

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            if (socket == null)
            {
                Log.ErrorAf("[Net] Host={0} Port={1} Listen Socket Error", host, port);
                return(false);
            }

            IPEndPoint listen_endpoint = new IPEndPoint(IPAddress.Parse(host), (int)port);

            try
            {
                socket.Bind(listen_endpoint);
                socket.Listen(listen_max_count);
                ListenParas paras = new ListenParas(socket, factory, host, port);
                socket.BeginAccept(on_accept, paras);
            }
            catch (System.Exception ex)
            {
                Log.ErrorAf("[Net] Host={0} Port={1} BeginAccept Error={0}", ex.ToString());
                return(false);
            }

            return(true);
        }