Check if network port is in use

Just starting out? Need help? Post your questions and find answers here.
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Check if network port is in use

Post by jacdelad »

Ist there a simple way to check if a network port is in use (so I don't try to use it) without interrupting someone who is using it? I found this: https://windowsloop.com/check-ports-in-use-windows-10/ which is a way on windows (via RunProgram and piping), but maybe there's a more elegant way.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Opcode
Enthusiast
Enthusiast
Posts: 138
Joined: Thu Jul 18, 2013 4:58 am

Re: Check if network port is in use

Post by Opcode »

GetTcpTable2 may fit the bill for Windows at least.

Something I just tossed together (may or may not work properly :lol:):

Code: Select all

Enumeration
  #TcpConnectionOffloadStateInHost
  #TcpConnectionOffloadStateOffloading
  #TcpConnectionOffloadStateOffloaded
  #TcpConnectionOffloadStateUploading
  #TcpConnectionOffloadStateMax
EndEnumeration

Structure MIB_TCPROW2
  dwState.l
  dwLocalAddr.l
  dwLocalPort.l
  dwRemoteAddr.l
  dwRemotePort.l
  dwOwningPid.l
  dwOffloadState.l
EndStructure

Structure MIB_TCPTABLE2
  dwNumEntries.l
  table.MIB_TCPROW2[256]
EndStructure

Import "Iphlpapi.lib"
  GetTcpTable2(*TcpTable, *SizePointer, Order.b)
EndImport

ulSize = 0
*pTcpTable.MIB_TCPTABLE2 = AllocateMemory(SizeOf(MIB_TCPTABLE2))

If (dwRetVal = GetTcpTable2(*pTcpTable, @ulSize, #True)) = #ERROR_INSUFFICIENT_BUFFER
  FreeMemory(*pTcpTable)
  *pTcpTable = AllocateMemory(ulSize)
EndIf

If (dwRetVal = GetTcpTable2(*pTcpTable, @ulSize, #True)) = #NO_ERROR
  For i = 0 To *pTcpTable\dwNumEntries
    Debug "IP: " + IPString(*pTcpTable\table[i]\dwLocalAddr)
    Debug "Port: " + Str(*pTcpTable\table[i]\dwLocalPort)
    Debug "Owning Process ID: " + Str(*pTcpTable\table[i]\dwOwningPid)
  Next
EndIf
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Check if network port is in use

Post by jacdelad »

Aye nice, that'll do it. Thanks very much!
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
plouf
Enthusiast
Enthusiast
Posts: 281
Joined: Fri Apr 25, 2003 6:35 pm
Location: Athens,Greece

Re: Check if network port is in use

Post by plouf »

However if you try to open the port, you are NOT interrupting
Just fail to open
Christos
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Check if network port is in use

Post by jacdelad »

This may be correct, but it's not elegant. Also, if I'm trying to find all ports in use it is time-costly and unnecessary to try every port.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Post Reply