13 July, 2014

How to create a windows service that listen to a TCP port

Hello,

I've been looking for quite a while now for a way to create a windows service that just answer on a TCP port that I configure, didn't needed it to do anything just answer...
I needed it to utilize our F5 load balancer in a way that our help desk guys could remove a server from the pool by stopping a windows service.

So, the searches ended here is the recipe for the windows service TCP port answerer:



The Ingredients:
1 iperf.exe from https://iperf.fr
1 command line
1 registry settings

How to:
First Download the files and save them in a local directory (lets say "C:\TCP_Service").

Then, run he following command:

sc create TCP_Service binPath= "C:\TCP_Service\srvany.exe" start= auto

You can replace the TCP_Service string (after the "create" word) with any name you want (you can run sc create /? for more options)

Then save the following lines between the "====" as a parameters.reg

=========================================
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\TCP_Service\Parameters]
"AppParameters"="-s -p 1234"
"Application"="C:\\TCP_Service\\iperf.exe"


=========================================

You can change the location and port (here it is 1234) to what ever you like (the location is the same location of the srvany.exe file from the previous step.

Double click on the parameters.reg file and answer yes to all.

Run services.msc and you will see a new service named TCP_Service, start it.
Open cmd and type: telnet 127.0.0.1 1234

see that it connects...


Enjoy...