我有一个侦听tcp/ip连接的Windows服务。 当我运行该服务时,我收到错误"通常只允许每个套接字地址(协议/网络地址/端口)使用一次"。 我可以在控制台应用程序中运行代码而不会出现问题。
服务启动并在自己的线程上创建侦听器:
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