I've been able to do this by using SKAN's RunCMD function and running "netstat -an" and searching for the port, but I'm wondering if there is a simpler way using DllCalls. I don't need to connect to the port - just check whether it is currently listening or established.
Cheers
Looking for a way to check if a particular port is currently listening Topic is solved
Re: Looking for a way to check if a particular port is currently listening
Try:
Code: Select all
SetTitleMatchMode, 2
Run, %A_ComSpec%,,, PID
WinWait, ahk_pid %PID%
ControlSend,, netstat -an{Enter}, cmd.exe
Re: Looking for a way to check if a particular port is currently listening Topic is solved
Thanks! I've actually just managed to cobble together after discovering the GetTcpTable WinAPI function
I found this old gem and butchered together a function that only checks for a particular port and returns its state if it exists.
Gem:
https://github.com/Ixiko/AHK-libs-and-classes-collection/blob/master/lib-a_to_h/GetTcpTable.ahk
Modified function:
I found this old gem and butchered together a function that only checks for a particular port and returns its state if it exists.
Gem:
https://github.com/Ixiko/AHK-libs-and-classes-collection/blob/master/lib-a_to_h/GetTcpTable.ahk
Modified function:
Code: Select all
TCP_PortExist(port) {
static hIPHLPAPI := DllCall("LoadLibrary", "str", "iphlpapi.dll", "ptr"), table := []
VarSetCapacity(TBL, 4 + (s := (20 * 32)), 0)
while (DllCall("iphlpapi\GetTcpTable", "ptr", &TBL, "uint*", s, "uint", 1) = 122)
VarSetCapacity(TBL, 4 + s, 0)
loop % NumGet(TBL, 0, "uint") {
o := 4 + ((A_index - 1) * 20)
, temp_port := (((ROW := NumGet(TBL, o+8, "uint"))&0xff00)>>8) | ((ROW&0xff)<<8)
, state := NumGet(TBL, o, "uint")
if (temp_port = port)
return state, DllCall("FreeLibrary", "ptr", hIPHLPAPI)
}
return 0, DllCall("FreeLibrary", "ptr", hIPHLPAPI)
}
/* ===============================================================================================================================
References:
- https://msdn.microsoft.com/en-us/library/aa366026(v=vs.85).aspx GetTcpTable function
- https://msdn.microsoft.com/en-us/library/aa366917(v=vs.85).aspx MIB_TCPTABLE structure
- https://msdn.microsoft.com/en-us/library/aa366909(v=vs.85).aspx MIB_TCPROW structure
State-Codes:
- CLOSED 1
- LISTEN 2
- SYN_SENT 3
- SYN_RCVD 4
- ESTAB 5
- FIN_WAIT1 6
- FIN_WAIT2 7
- CLOSE_WAIT 8
- CLOSING 9
- LAST_ACK 10
- TIME_WAIT 11
- DELETE_TCB 12
*/
Re: Looking for a way to check if a particular port is currently listening
May any one please edit this function to return remote port instead of local port. Thanks