windows service access to network before logon

255 views Asked by At

I wrote a windows service in Java, and it starts automatically (before user login to Windows). My problem is that it looks like the service has no access to the network before logon.

In the logs I see that before logon, I get: java.net.UnknownHostException. However, the same code works fine after logon.

I tried to define the service as "network service", it did not helped.

Can anyone please help?

BTW:

  1. There is no WiFi involved

  2. I looked how TeamViewer program works and I saw that they have some registry values in FirewallPolicy. and they defined their service as local system. Which made me think maybe it has something to do with the firewall settings, however I cannot find anything in the web about it.

2

There are 2 answers

0
Kuf On BEST ANSWER

I just managed to solve this. The issue was that the service started before there was access to the internet. Keep in mind that the network might be accessible but the internet isn't.

I solved it by doing: (pseudo code)

while (false) {
    check if http://www.example.com is accesible:
        if true - break
        if false - sleep for 3 seconds
}
... (rest of the code)
0
Vikas Gupta On

If the exception one gets is UnknownHostException, this would imply that somehow DNS resolution failed..

Note further that DNS resolution itself depends on IP. Hence, if the machine in question is slow to connect to the respective gateway (router), then DNS resolution has no chance of succeeding.

Hence, while you may try to resolve the host resolution in a loop with a sleep (as suggested by Kuf).. I'd suggest try to log how soon does the machine gets the IP address after the reboot (a sign of how soon it gets connected to the Network), and then go from there (additional SuperUser or ServerFault question perhaps).