System.Net.Sockets.TcpListener.AcceptSocket C# (CSharp) Méthode

AcceptSocket() public méthode

public AcceptSocket ( ) : Socket
Résultat Socket
        public Socket AcceptSocket()
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(this);

            if (!_active)
            {
                throw new InvalidOperationException(SR.net_stopped);
            }

            Socket socket = _serverSocket.Accept();

            if (NetEventSource.IsEnabled) NetEventSource.Exit(this, socket);
            return socket;
        }

Usage Example

        //Clienter der kan tilkoble serveren
        private void Run()
        {
            string productInformation = Console.ReadLine();

            try
            {
                string[] splitProduct = productInformation.Split('|');
                _product = new Product(splitProduct[0], double.Parse(splitProduct[1]), splitProduct[2]);
                Console.WriteLine("[" + GetDatetime() + "] Server: Produktet er blevet oprettet");
            }
            catch (Exception)
            {
                Console.WriteLine("[" + GetDatetime() + "] Server: Du har indtastet produktet i et ugyldigt format! Prøv igen.");
                Run();
            }

            TcpListener _tcpListener = new TcpListener(IPAddress.Any, _port);
            _tcpListener.Start();

            while (true)
            {
                Socket socket = _tcpListener.AcceptSocket();
                _clientIP = socket.RemoteEndPoint as IPEndPoint;

                Console.WriteLine("[" + GetDatetime() + "] Server: {0} er forbundet til serveren.", _clientIP.Address);
                ClientHandler clientHandler = new ClientHandler(socket, _product, _service, _clientIP);
                Thread clientThread = new Thread(clientHandler.Run);
                clientThread.Start();
            }
        }
All Usage Examples Of System.Net.Sockets.TcpListener::AcceptSocket