Determine which ports are in use

Everything else that doesn't fall into one of the other PB categories.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Determine which ports are in use

Post 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
~I see one problem with your reasoning: the fact is thats not a chicken~
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

*bump*
~I see one problem with your reasoning: the fact is thats not a chicken~
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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")
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post 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.
~I see one problem with your reasoning: the fact is thats not a chicken~
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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).
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post 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?
~I see one problem with your reasoning: the fact is thats not a chicken~
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

just try to bind one, if you cannot it is in use :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

Hmmm, is there no other way?
~I see one problem with your reasoning: the fact is thats not a chicken~
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

Does this help (GetTcpTable_() and GetUdpTable_() in particular)?
http://www.codeproject.com/internet/tcpinfo.asp
Mat
Post Reply