Page 1 of 1
Determine which ports are in use
Posted: Mon Aug 29, 2005 11:39 pm
by Killswitch
Hey,
How can I tell which ports are in use on the computer my program is running on, without trying to create a server on each one. Also how can I check which ports are in use on a remote computer?
Thanls a lot,
Killswitch
Posted: Tue Aug 30, 2005 8:52 pm
by Killswitch
*bump*
Posted: Tue Aug 30, 2005 11:22 pm
by PB
Have a bit of patience, you don't need to bump after one single day... :roll:
A web search for "vb check open ports" and you'll see many Visual Basic
examples which should be easy to convert to PureBasic. Indeed, the first
Google match leads to this page...
http://vbforums.com/showthread.php?p=21 ... ost2137718
...which explains that executing a redirected NetStat command to a file will
create a list that you can parse. So here's a working procedure for you:
Code: Select all
Procedure SaveActivePorts(file$)
a$=Space(255)
If OSVersion()<#PB_OS_Windows_2000
GetWindowsDirectory_(a$,255)
Else
GetSystemDirectory_(a$,255)
EndIf
c$=Space(255) : GetEnvironmentVariable_("comspec",@c$,255)
RunProgram(c$,"/c "+a$+"\netstat.exe -a -n > "+Chr(34)+file$+Chr(34),"",1|2)
EndProcedure
SaveActivePorts("c:\Ports.txt")
Posted: Tue Aug 30, 2005 11:26 pm
by Killswitch
Nuts I didn't want to use the netstat command, that's how i've done this in the past. I was hoping for a API command or something similar. Sorry about the bump, but on my computer the thread was slipping down the page and I am a bit eger for a solution.
Posted: Tue Aug 30, 2005 11:47 pm
by PB
> I didn't want to use the netstat command
Just curious: why not? It's on every Windows platform, or unless your app
is intended for Linux/MacOS too (but I doubt it, since you mention API).
Posted: Wed Aug 31, 2005 12:54 am
by Killswitch
To be honest it feels like cheating. If you use a batch file then you're basically making something else do all the mokey work - I kinda like the idea of writing it all myself. Then again I suppose you could pretty much say the same for an API command.
Still are there any other solutions?
Posted: Wed Aug 31, 2005 9:47 am
by blueznl
just try to bind one, if you cannot it is in use

Posted: Wed Aug 31, 2005 10:44 am
by Killswitch
Hmmm, is there no other way?
Posted: Wed Aug 31, 2005 11:05 am
by MrMat
Does this help (GetTcpTable_() and GetUdpTable_() in particular)?
http://www.codeproject.com/internet/tcpinfo.asp