delphi winsock 获取计算机名和IP


本文整理自网络,侵删。

 
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    lbl1: TLabel;
    edt1: TEdit;
    btn1: TButton;
    lbl2: TLabel;
    edt2: TEdit;
    btn2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
  private
    { Private declarations }
    function WinSockInit: BOOL; 
    procedure WinSockUnInit;

    function GetLocalHostName: string;
    function GetIpAddress: string;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  WSAData: TWSAData;

const
  Version: Word = $0202;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.btn1Click(Sender: TObject);
begin
  edt1.Text := GetLocalHostName;
end;

procedure TForm1.btn2Click(Sender: TObject);
begin
  edt1.Text := GetLocalHostName;
  edt2.Text := GetIpAddress;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  WinSockInit;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  WinSockUnInit;
end;

function TForm1.GetIpAddress: string;
type
  TapInAddr = array [0..10] of PInAddr;
  PapInAddr = ^TapInAddr;

var
  RemoteHost : PHostEnt;
  InAddr: PapInAddr;
  HostName: array [0..255] of Char;
  i: Integer;
begin
  gethostname(HostName, SizeOf(HostName));
  RemoteHost := gethostbyname(HostName);
  if RemoteHost = nil then
  begin
    Result := '127.0.0.1';
    Exit;
  end;
  InAddr := PapInAddr(RemoteHost^.h_addr_list);
  i := 0;
  while InAddr^[i] <> nil do
  begin
    Result := StrPas(inet_ntoa(InAddr^[i]^));
    Inc(i);
  end;
end;

function TForm1.GetLocalHostName: string;
var
  HostName: array [0..255] of Char;
begin
  gethostname(HostName,SizeOf(HostName));
  Result := HostName;
end;

function TForm1.WinSockInit: BOOL;
begin
  if WSAStartup(Version, WSAData) = ERROR_SUCCESS then
  begin
//    ShowMessage('WinSock库记载成功!');
    Result := True;
  end
  else
  begin
    ShowMessage('WinSock库记载失败!');
    if WSAIsBlocking = True then
      WSACancelBlockingCall;
    WSACleanup;
  end;
  
end;

procedure TForm1.WinSockUnInit;
begin
  if WSACleanup = ERROR_SUCCESS then
  begin
//    ShowMessage('释放winsock库成功');
  end
  else
  begin
    ShowMessage('释放winsock库失败');
    Exit;
  end;
end;

end.

相关阅读 >>

Delphi xe6 读取android设备联系人

Delphi 根据进程名得到进程所在路径

Delphi 截取字符串的用法

Delphi 一行关键代码阻止360云查杀

Delphi文件 fileopen 、fileseek等的用法(看红色字体)

crc16unt.pas

Delphi 批量把图片保存到access数据库

Delphi程序在每个windows 会话中只执行一次

Delphi 设置webbrowser 代理服务器 与 useragent

Delphi showdebuginfo 窗口

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...