Lazarus中文社区

 找回密码
 立即注册(注册审核可向QQ群索取)

QQ登录

只需一步,快速开始

Lazarus IDE and 组件 下载地址版权申明
查看: 3076|回复: 1

获取外网地址的方法(使用Synapse and lnet)

[复制链接]

该用户从未签到

发表于 2013-1-26 19:03:13 | 显示全部楼层 |阅读模式
  1. {$mode objfpc}{$H+}

  2. uses
  3.   fphttpclient;

  4. begin
  5.   with TFPHTTPClient.Create(nil) do
  6.     try
  7.       WriteLn(Get('http://automation.whatismyip.com/n09230945.asp'));
  8.     finally
  9.       Free;
  10.     end;
  11. end.
复制代码
你可以使用 http://ifconfig.me/ip 使用 LNet 和 HTTPClient 来获取:
  1. function TForm1.HTTPClientInput(ASocket: TLHTTPClientSocket; ABuffer: pchar;
  2.   ASize: integer): integer;
  3. var
  4.   oldLength: dword;
  5.   HTTPBuffer: string;
  6. begin
  7.   oldLength := Length(HTTPBuffer);
  8.   setlength(HTTPBuffer,oldLength + ASize);
  9.   move(ABuffer^,HTTPBuffer[oldLength + 1], ASize);
  10.   MemoHTML.Text := HTTPBuffer;
  11.   MemoHTML.SelStart := Length(HTTPBuffer);
  12.   Result := aSize; // tell the http buffer we read it all
  13. end;   
复制代码
回复

使用道具 举报

该用户从未签到

发表于 2013-2-16 19:37:02 | 显示全部楼层
lazarus在linux下通过调用外部脚本获取外网IP

type
  TResultStatus = record
    ExitStatus: Integer;
    StatusStr: string;
  end;
  
function RunExternalFuncS(FuncName: string; ParaStr: string=''): TResultStatus;
var
  i: Integer;
  AProcess: TProcess;
  AStringList: TStringList;
begin
  Result.ExitStatus := -1;
  Result.StatusStr := '';

  AProcess := TProcess.Create(nil);
  AStringList := TStringList.Create;

  AProcess.CommandLine := FuncName + ' ' + ParaStr;
  AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
  try
    try
      AProcess.Execute;
      Result.ExitStatus:= AProcess.ExitStatus;
    except
     
    end;
    AStringList.LoadFromStream(AProcess.Output);
    for i := 0 to AStringList.Count - 1 do
      if Trim(AStringList) <> '' then
        begin
          Result.StatusStr := Trim(AStringList);
          Break;
        end;
  finally
    AStringList.Free;
    AProcess.Free;
  end;
end;


function GetPublicNetIP(): string;
var
  rs: TResultStatus;
begin
  Result:= '';
  rs:= RunExternalFuncS('/opt/getpublicnetip');
  if (rs.ExitStatus = 0)  then Result:= rs.StatusStr;
end;   



#!/bin/bash
#Linux下的脚本文件(getpublicnetip)

NEWIP=''

NEWIP=`curl -s -S -t 6 -m 6 http://checkip.dyndns.org`

errorcode=$?
if [ $errorcode -ne 0 ]; then
  echo "don't get publicnetip"
  exit $errorcode
fi

NEWIP=`echo $NEWIP | sed 's/.*Current IP Address: \([0-9\.]*\).*/\1/g'`

if [ "X$NEWIP" != "X" ]; then
  echo "${NEWIP}"
  exit 0
else
  echo "ip is null"
  exit 1
fi

点评

感谢分享,谢谢  发表于 2013-2-18 18:47
回复 支持 反对

使用道具 举报

*滑块验证:

本版积分规则

QQ|手机版|小黑屋|Lazarus中国|Lazarus中文社区 ( 鄂ICP备16006501号-1 )

GMT+8, 2025-5-2 09:49 , Processed in 0.030866 second(s), 12 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表