SocketException prevents C # TCPListener from being used on Windows Service

I have a windows service that does the following when it starts. When run through a console application, it works fine, but as soon as I put the windows service, I get the following exception. Here's what I've tried so far:

  • Disabled firewall, also tried to add explicit exceptions for exe, port and protocol
  • Verified CAS Policy Config, Shows Unlimited Rights
  • Configuring the service to run as an administrator account, local system, local service, and network service, each with the same result
  • Tried different ports
  • Also tried 127.0.0.1 just to look ... same thread

This is ruining my head, so any help would be greatly appreciated:

The code:

var _listener = new TcpListener(endpoint); //192.168.2.2:20000
_listener.Start();

      

Received exception:

Service cannot be started. System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions
   at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Bind(EndPoint localEP)
   at System.Net.Sockets.TcpListener.Start(Int32 backlog)
   at System.Net.Sockets.TcpListener.Start()
   at Server.RequestHandler.StartServicingRequests(IPEndPoint endpoint)
   at Server.Server.StartServer(String[] args)
   at Server.Server.OnStart(String[] args)
   at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

      

+2


a source to share


2 answers


I recommend that you bind to the endpoint using IPAddress.Any (0.0.0.0) and a specific port.



The error message is likely because another application has already opened this port or recently opened it.

+5


a source


More information on the error code on MSDN .



0


a source







All Articles