¿Cómo soluciono el error "Normalmente solo se permite un uso de cada dirección de socket (protocolo / dirección de red / puerto)"?

He hecho un montón de googlear, pero no he tenido mucha suerte con mis problemas. Soy nuevo en la programación de red y al intentar aprender, he intentado configurar un servidor y un cliente sencillos que se comunican (siguiendo un tutorial en línea que se encuentra aquí ->http://tech.pro/tutorial/704/csharp-tutorial-simple-threaded-tcp-server)

El problema que tengo es que sigo recibiendo la excepción "Normalmente, solo se permite un uso de cada dirección de socket (protocolo / dirección de red / puerto)" cuando se intenta iniciar TcpListener en el servidor.

He intentado desactivar mi firewall, cambiando el puerto que se va a usar, moviendo las variables pero sin éxito (el cliente funciona bien, pero obviamente no puede encontrar el servidor porque no puedo iniciarlo).

He visto soluciones que describen el uso de Socket.Poll () pero como solo uso el objeto TcpListener, no tengo idea de cómo usar la función de Encuesta.

Mi código:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Text;

namespace ServerTutorial {
class Server {
    private readonly Thread m_listenThread;

    public Server() {
        m_listenThread = new Thread(new ThreadStart(ListenForClients));
        m_listenThread.Start();
    }

    public void ListenForClients() {
        var listener = new TcpListener(IPAddress.Any, 3000);
        listener.Start();

        while (true) {
            //Blocks until a client has connected to the server
            TcpClient client = listener.AcceptTcpClient();

            //Send a message to the client
            var encoder = new ASCIIEncoding();
            NetworkStream clientStream = client.GetStream();
            byte[] buffer = encoder.GetBytes("Hello Client!");
            clientStream.Write(buffer, 0, buffer.Length);
            clientStream.Flush();

            //Create a thread to handle communication with the connected client
            var clientThread = new Thread(new ParameterizedThreadStart(HandleClient));
            clientThread.Start(client);
        }
    }

    private void HandleClient(object clientObj) { //Param thread start can only accept object types, hence the cast
        var client = (TcpClient) clientObj;
        NetworkStream clientStream = client.GetStream();

        var message = new byte[4096];

        while (true) {
            int bytesRead = 0;

            try {
                //Block until a client sends a message
                bytesRead = clientStream.Read(message, 0, 4096);
            } catch {
                //A socket error has occurred
                System.Diagnostics.Debug.WriteLine("A socket error has occured");
                break;
            }

            if (bytesRead == 0) {
                //The client has disconnected from the server
                System.Diagnostics.Debug.WriteLine("A client has disconnected from the server");
                client.Close();
                break;
            }

            //Message has been received
            var encoder = new ASCIIEncoding();
            System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
        }
    }
}
}

En mi método principal:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ServerTutorial {
class Program {
    static void Main(string[] args) {
        var server = new Server();
        server.ListenForClients();
    }
}
}

Cualquier ayuda es muy apreciada!

Respuestas a la pregunta(3)

Su respuesta a la pregunta