Пример #1
0
        public void Listen()
        {
            try
            {
                var ipHostInfo = Dns.GetHostEntry(Config.Host);
                var ipAddress  = ipHostInfo.AddressList
                                 .First(ip => ip.AddressFamily == AddressFamily.InterNetwork);
                var localEndPoint = new IPEndPoint(ipAddress, Config.Port);
                var listener      = new Socket(ipAddress.AddressFamily,
                                               SocketType.Stream, ProtocolType.Tcp);

                listener.Bind(localEndPoint);
                listener.Listen(100);
                while (true)
                {
                    AllDone.Reset();
                    listener.BeginAccept(
                        AcceptCallback,
                        listener);
                    AllDone.WaitOne();
                }
            }
            catch (Exception e)
            {
                Abort(null, e);
            }
        }
Пример #2
0
        public static void StartListening()
        {
            // Establish the local endpoint for the socket.
            // The DNS name of the computer
            // running the listener is "host.contoso.com".
            IPHostEntry ipHostInfo    = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress   ipAddress     = ipHostInfo.AddressList[0];
            IPEndPoint  localEndPoint = new IPEndPoint(ipAddress, Port);

            // Create a TCP/IP socket.
            Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            // Bind the socket to the local endpoint and listen for incoming connections.
            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(100);

                Sistem.printF(Sistem.GetLogTag(Sistem.EnumLogTags.SERVER) + "Async Socket Server  ONLINE at " + ipAddress.ToString() + ":" + Port, ConsoleColor.Green);

                while (true)
                {
                    // Set the event to nonsignaled state.
                    AllDone.Reset();

                    // Start an asynchronous socket to listen for connections.
                    Sistem.printF(Sistem.GetLogTag(Sistem.EnumLogTags.SERVER) + "Waiting for a connection...", ConsoleColor.Gray);
                    listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);

                    // Wait until a connection is made before continuing.
                    AllDone.WaitOne();
                }
            }
            catch (Exception e)
            {
                Sistem.printF(e.ToString(), ConsoleColor.Red);
            }

            Sistem.printF("\nPress ENTER to continue...", ConsoleColor.Gray, false, false, true, true, true);
        }