得到本机当前网络连接的方法

    技术2022-05-11  133

    unit ips;

    interface

    uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls, winsock;

    type

    function WSAIoctl( hSocket: TSocket; ControlCode:dword;                   InBuf : Pointer; InBufLen:DWord;                   OutBuf : Pointer; OutBufLen:DWord;                   BytesReturned : PDWord;                   lpOverlapped: POverlapped;                   lpOverlappedRoutine:pointer) : Integer; stdcall;

    function WSASocket(Family, sType, Protocol : Integer;                   lpProtocolInfo : Pointer;                   Group : uint;                   dwFlags : DWORD): TSocket; stdcall;

    function WSAIoctl;          external     'ws2_32.dll' name 'WSAIoctl';function WSASocket;         external     'ws2_32.dll' name 'WSASocketA';

    function GetAvailIp:TStrings;function GetCardBindingIp : TStrings;

    function GetConnectionIp : string;

    implementation

    {$R *.dfm}

    function GetAvailIp:TStrings;type   TaPInAddr = array [0..10] of PInAddr;   PaPInAddr = ^TaPInAddr;var    phe: PHostEnt;    pptr: PaPInAddr;    Buffer: array [0..63] of char;    i: Integer;    GInitData: TWSADATA;begin    result := TStringList.Create;    WSAStartup($101, GInitData);    GetHostName(Buffer, SizeOf(Buffer));    phe :=GetHostByName(buffer);    if phe = nil then Exit;    pptr := PaPInAddr(Phe^.h_addr_list);    i := 0;    while pptr^[i] <> nil do    begin      result.add(StrPas(inet_ntoa(pptr^[i]^)));      Inc(i);    end;    WSACleanup;end;

    Function getCardBindingIp : TStrings;const   SIO_GET_INTERFACE_LIST : dword = 1074033791;type  _INTERFACE_INFO = record    iiFlags            : ulong;      //* Type and status of the interface */    iiAddress          : TSockaddr;  //* Interface address */    iiBroadcastAddress : TSockaddr;  //* Broadcast address */    iiNetmask          : TSockaddr;  //* Network mask */  end;var    pAddrInet : TSockAddr;    OutBufLen,    RecvBytes,    i         : DWORD;    Buffer: array [0..63] of char;    wsError   : Integer;    WSAData   : TWSAData;    MySocket  : TSocket;    localAddr : Array[1..10] of _INTERFACE_INFO; //up to 10 NICsbegin  result := TStringList.Create;

      wsError := Winsock.WSAStartup(2,  WSAData);  If wsError <> 0 then  exit;

      Gethostname(Buffer, 64);  MySocket := WSASocket(AF_INET, Sock_DGRAM, IPPROTO_UDP, nil,0,0);  If MySocket = INVALID_SOCKET then exit;  OutBufLen := Sizeof(localAddr);  RecvBytes := OutBufLen;  FillChar(LocalAddr,OutBufLen,0);  wsError := WSAIoctl(MySocket,SIO_GET_INTERFACE_LIST,nil,0,@localAddr,OutBufLen,@RecvBytes,nil,nil);  if wsError = SOCKET_ERROR then  begin    wsError := WSAGetLastError;    Raise Exception('Socket error:'+IntToStr(wsError));    exit;  end;  for i := 1 to RecvBytes div Sizeof(_Interface_Info) do  begin      pAddrInet := localAddr[i].iiAddress;      result.add(inet_ntoa(pAddrInet.sin_addr));  end;  closesocket(MySocket);  WSACleanup();end;

    function GetConnectionIp : string;var  avail,binding : TStrings;  i ,j : integer;begin  result := '';

      avail := getAvailIp;  binding := getCardBindingIp;

      for i := 0 to avail.Count - 1 do  begin    for j := 0 to binding.Count - 1 do      begin        if avail[i] = binding[j] then        begin          result := avail[i];          exit;        end;      end;  end;end;


    最新回复(0)