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

该博客主要展示了IP获取相关函数的实现代码。定义了WSAIoctl、WSASocket等函数,还实现了GetAvailIp、GetCardBindingIp和GetConnectionIp等获取IP的函数,通过调用Winsock库中的相关方法,完成IP地址的获取和处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 NICs
begin
  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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值