[fpc-pascal] lNet getting the local IP

Henry Vermaak henry.vermaak at gmail.com
Wed Oct 6 16:09:29 CEST 2010


On 06/10/10 14:27, Felipe Monteiro de Carvalho wrote:
> Ok, thanks everyone, it seams that I managed to extract a function
> from Silvio's code which doesn't use Synapse. I only tested in Windows
> so far:
>
> unit chesstcputils;
>
> {$mode objfpc}{$H+}
>
> interface
>
> uses
>    {$IFDEF MSWINDOWS}
>    Winsock,
>    {$ENDIF}
>    Classes, SysUtils;
>
> function ChessGetLocalIP(): string;
>
> implementation
>
> const
>    CFormatIPMask = '%d.%d.%d.%d';
>
> function ChessGetLocalIP(): string;
> var
>    I, VAttempt: Integer;
>    VStrTemp, VSitesToTry: TStringList;
> {$IFDEF UNIX}
>    VProcess: TProcess;
> {$ENDIF}
> {$IFDEF MSWINDOWS}
> var
>    VWSAData: TWSAData;
>    VHostEnt: PHostEnt;
>    VName: string;
> {$ENDIF}
> begin
>    Result := '';
> {$IFDEF UNIX}
>        VStrTemp := TStringList.Create;
>        VProcess := TProcess.Create(nil);
>        try
>          VProcess.CommandLine :=
>            'sh -c "ifconfig eth0 | awk ''/inet end/ {print $3}''"';

Yuck.  This doesn't work on my system (debian).  If you really want the 
least effort, you may have more luck with simply parsing `hostname -I` 
somehow.  The right way to do this is with an ioctl, I believe 
(SIOCGIFCONF).  Look here:

http://www.kernel.org/doc/man-pages/online/pages/man7/netdevice.7.html

I'm sure there's some code floating around, but it probably means that 
you have to translate some headers :(.

Henry



More information about the fpc-pascal mailing list