I am writing a program which would determine the process id of the port in C#.Net.
There is a function GetExtendedTcpTable in IpHlpApi.dll which returns TcpTable. After getting the table, I am iterating through the rows and parsing the port number.
Now for comparing the Port numbers, I am converting 27144 Port number to network byte order using IPAddress.HostToNetworkOrder and comparing with other Port number using Marshal.ReadInt32. The comparison is not working. i.e. No result match.
If instead of using IPAddress.HostToNetworkOrder, I use the following logic
((27144 & 0xFF) << 8) + ((27144 & 0xFF00) >> 8) == Marshal.ReadInt32(portAddress)
I am able to determine the process id i.e. one of the results match.
Can you please tell me why IPAddress.HostToNetworkOrder is not working?
You’re probably using the
DWORDfromdwLocalPort/dwRemotePortin TcpRow. The DWORD is certainly not going to be 16-bits, and you’ll likely call the 32-bit version of IPAddress.HostToNetworkOrder… you should try to cast it as anInt16before using theIPAddress.HostToNetworkOrderfunction.