views:

316

answers:

3

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)
A: 

to build the whole tcp protocol use .Net Protocol Builder from www.protocol-builder.com basicly it generated the tcp protocol with the client/server connections and the packets you nay build, but I am not sure if it works in a service ...

Zamir
A: 

I recommend you bind to an endpoint with IPAddress.Any (255.255.255.255) and a specific port.

The error message is likely because another application already has that port open or has recently had it open exclusively.

Stephen Cleary
Thanks, I will give that a try
JoeGeeky
A: 

More information on the error code is on MSDN.

Stephen Cleary