Windows TCP/IP 侦听器服务 "Only one usage of each socket address (protocol/network address/port) is normally permitted"

Windows TCP/IP Listener Service "Only one usage of each socket address (protocol/network address/port) is normally permitted"

我有一个 Windows 服务侦听 tcp/ip 连接。当我 运行 服务时,我收到错误 "Only one usage of each socket address (protocol/network address/port) is normally permitted"。我可以 运行 控制台应用程序中的代码没有问题。

服务开始并在其自己的线程上创建侦听器:

 protected override void OnStart(string[] args)
    {
        this.tcpListener = new TcpListener(IPAddress.Parse(IpAddress.Any), 8100);
        this.listenThread = new Thread(new ThreadStart(ListenForClients));
        this.listenThread.Start();
    }

启动侦听器:

private void ListenForClients()
    {
        this.tcpListener.Start(); //<-----Produces the error
        System.Threading.Thread.Sleep(5000);
        this.tcpListener.Stop();
    }

我刚刚测试了您的代码,它可以在我的电脑上运行。 如果您想知道哪个程序正在使用您计算机上的那个端口,我建议您使用 Microsoft 开发的 SysInteralSuite 中的 "tcpview" 工具。

你可以在这里找到它。 https://technet.microsoft.com/en-us/sysinternals/bb842062.aspx