发信人: redfox (Z掉了几乎所有的版...), 信区: BorlandDev
标  题: Delphi编程 -- 9.3 得到本机IP地址
发信站: 哈工大紫丁香 (2000年09月01日16:42:13 星期五), 站内信件

得到本机IP地址?  
How about using winsockets?

This code is untested and ugly.
program get_ip;
uses
  winsock,sysutils;
VAR
  ch : ARRAY[1..32] OF Char;
  i : Integer;
  WSData: TWSAData;
  MyHost: PHostEnt;
begin
  IF WSAstartup(2,wsdata)<>0 THEN
    BEGIN
      Writeln('can''t start Winsock: Error ',WSAGetLastError);
      Halt(2);
    END;
  try
    IF getHostName(@ch[1],32)<>0 THEN
      BEGIN
        Writeln('getHostName failed');
        Halt(3);
      END;
  except
    Writeln('getHostName failed');
    halt(3);
  end;
  MyHost:=GetHostByName(@ch[1]);
  IF MyHost=NIL THEN
    BEGIN
      Writeln(GetHostName('+StrPas(@ch[1])+') failed : Error
'+IntToStr(WSAGetLastError));
      Halt(4);
    END
  ELSE
    BEGIN
        Write('address ');
         FOR i:=1 TO 4 DO
            BEGIN
              Write(Ord(MyHost.h_addr^[i-1]));
              IF i<4 THEN
                write('.')
              ELSE
                writeln;
            END;
   END;
end.


 

--
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.227.107]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.523毫秒