Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4024852
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:46:24+00:00 2026-05-20T10:46:24+00:00

When I establish a socket connection to a server, both client and server have

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:46:24+00:00Added an answer on May 20, 2026 at 10:46 am

    you are correct about use the InternetQueryOption function with the INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO flag, this will return a INTERNET_DIAGNOSTIC_SOCKET_INFO Structure

    typedef struct {
      DWORD_PTR Socket;
      DWORD     SourcePort;
      DWORD     DestPort;
      DWORD     Flags;
    } INTERNET_DIAGNOSTIC_SOCKET_INFO, * LPINTERNET_DIAGNOSTIC_SOCKET_INFO;
    

    which in Delphi look like this

    PINTERNET_DIAGNOSTIC_SOCKET_INFO = ^TINTERNET_DIAGNOSTIC_SOCKET_INFO;
    TINTERNET_DIAGNOSTIC_SOCKET_INFO=  record
      Socket     : DWORD_PTR;
      SourcePort : DWORD;
      DestPort   : DWORD;
      Flags      : DWORD;
    end;
    

    and then you can wrote a function to return the socket info

    function GetSocketInfo(hInet: HINTERNET) : TINTERNET_DIAGNOSTIC_SOCKET_INFO;
    var
      lpdwBufferLength: DWORD;
    begin
       lpdwBufferLength:=SizeOf(TINTERNET_DIAGNOSTIC_SOCKET_INFO);
       ZeroMemory(@Result,lpdwBufferLength);
       if not InternetQueryOption(hInet, INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO, @Result, lpdwBufferLength) then
        RaiseLastOSError;
    end;
    

    check this sample app to see how use it.

    {$APPTYPE CONSOLE}
    
    uses
      Windows,
      WinInet,
      SysUtils;
    
    type
    
    PINTERNET_DIAGNOSTIC_SOCKET_INFO = ^TINTERNET_DIAGNOSTIC_SOCKET_INFO;
    TINTERNET_DIAGNOSTIC_SOCKET_INFO=  record
      Socket     : DWORD_PTR;
      SourcePort : DWORD;
      DestPort   : DWORD;
      Flags      : DWORD;
    end;
    
    const
      INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO = Cardinal(67);
    
    function GetSocketInfo(hInet: HINTERNET) : TINTERNET_DIAGNOSTIC_SOCKET_INFO;
    var
      lpdwBufferLength: DWORD;
    begin
       lpdwBufferLength:=SizeOf(TINTERNET_DIAGNOSTIC_SOCKET_INFO);
       ZeroMemory(@Result,lpdwBufferLength);
       if not InternetQueryOption(hInet, INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO, @Result, lpdwBufferLength) then
        RaiseLastOSError;
    end;
    
    //this a dummy function to download a file, only to show the use of the INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO
    procedure WinInet_HttpGet(const Url: string);
    const
    BuffSize = 1024*1024;
    var
      hInter   : HINTERNET;
      UrlHandle: HINTERNET;
      BytesRead: DWORD;
      Buffer   : Pointer;
      SocketInfo: TINTERNET_DIAGNOSTIC_SOCKET_INFO;
    begin
      hInter := InternetOpen('', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
      if Assigned(hInter) then
      begin
        GetMem(Buffer,BuffSize);
        try
            UrlHandle := InternetOpenUrl(hInter, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0);
            if Assigned(UrlHandle) then
            begin
               //Get the info of the socket
               SocketInfo:=GetSocketInfo(UrlHandle);
               Writeln('Socket Info');
               Writeln(Format('Source Port %d',[SocketInfo.SourcePort]));
               Writeln(Format('Dest   Port %d',[SocketInfo.DestPort]));
              try
                repeat
                  InternetReadFile(UrlHandle, Buffer, BuffSize, BytesRead);
                  if BytesRead>0 then
                  begin
                    //do your stuff
                  end;
                until BytesRead = 0;
              finally
               InternetCloseHandle(UrlHandle);
              end;
    
            end;
        finally
          FreeMem(Buffer);
        end;
        InternetCloseHandle(hInter);
      end
    end;
    
    
    begin
      try
        WinInet_HttpGet('http://msdn.microsoft.com/en-us/library/aa385096%28v=vs.85%29.aspx');
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
      readln;
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HI folks, I'm trying to establish connection to a remote server using ruby socket
I have a server socket that I setup, and a client socket that connects
I want to establish socket connection to streaming server (with iphone ) and want
A socket client program establishes a connection with the server, writes some bytes and
I'm trying to build a websocket server where each client establish its own redis
I'm looking to establish some kind of socket/COMET type functionality from my server(s) to
I am trying to conceptually work through a model for a client-server socket application
I'm trying to implement a C# web socket server, but its giving me a
I have a typical server in my end and a friend using a client
I am using the BLIP/MYNetwork library to establish a basic tcp socket connection between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.