SocketException preventing use of C# TCPListener in Windows Service

Posted by JoeGeeky on Stack Overflow See other posts from Stack Overflow or by JoeGeeky
Published on 2010-05-06T09:29:17Z Indexed on 2010/05/08 22:18 UTC
Read the original article Hit count: 565

Filed under:
|
|
|
|

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:

  • Disabled the firewall, also tried adding explicit exclusions for the exe, port, and protocol
  • Checked CAS Policy Config, shows unrestricted rights
  • Configured 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 see... same issue

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)

© Stack Overflow or respective owner

Related posts about c#

Related posts about tcplistener