When I establish a socket connection to a server, both client and server have sockets opened. Its easy to know what is the server port (since I use it to connect to the server). But I would like to discover the client port of the connection after connecting to a server. I am using Wininet functions in a Delphi 2010 application.
Pseudo-code:
1 – InternetOpen
2 – InternetConnect
3 – HttpOpenRequest
4 – HttpSendRequestA
5 – InternetReadFile
6 – ?????? <—— How to get the client port?
Edited:
I have found I should use InternetQueryOption with INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO, but i have no idea how to do that.
you are correct about use the
InternetQueryOptionfunction with theINTERNET_OPTION_DIAGNOSTIC_SOCKET_INFOflag, this will return aINTERNET_DIAGNOSTIC_SOCKET_INFO Structurewhich in Delphi look like this
and then you can wrote a function to return the socket info
check this sample app to see how use it.