Reputation: 3796
I have a Windows Service that does the following when started. When running via a Console application it works fine, but once I put in a Windows Service I get the below exception. Here is what I have tried so far:
This is wrecking my head, so any help would be greatly appreciated:
The Code:
var _listener = new TcpListener(endpoint); //192.168.2.2:20000
_listener.Start();
The resulting 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)
Upvotes: 2
Views: 9927
Reputation: 456342
I recommend you bind to an endpoint with IPAddress.Any (0.0.0.0) and a specific port.
The error message is likely because another application already has that port open or has recently had it open exclusively.
Upvotes: 5