views:

43

answers:

3

I have created a program in visual c++, where i have implemented a web service. The web service is set to listen on port 80, but if another program already is using this port, the web service fail to start up.

So when the webservice can't start, I would like to have a function or method, which can get the name of the process, that currently uses port 80. Then i can print an error to the user, and ask him to close the process.

A: 

I would consider, as a first attempt, running netstat as an external process and capturing/parsing the output. It gives you the active connections.

paxdiablo
A: 

Not sure if there is a way to do this via an API (not a windows programmer), however you could try netstat -abo as a shell command, then look for TCP and port 80 in the resulting string, and you'll have the binary name...

EDIT: I believe you need XP SP2 atleast for this to work...

Nim
+2  A: 

GetExtendedTcpTable and GetExtendedUdpTable give you a list of network connections. You can walk through this list and check if a program is using port 80 (it provides process IDs as well).

wj32