-
2011. 6. 10. 01:37 프로그래밍 언어/Pascal
소켓 함수를 이용하여 네트워크 아이피 를 가져옵니다.
헤더파일은 JEDI 프로젝트 것을 사용 했습니다.
Delphi - JEDI API Header Library
MSDN : gethostname, gethostbyname
함수 사용 예제
아이피 목록 파싱 함수
헤더파일은 JEDI 프로젝트 것을 사용 했습니다.
Delphi - JEDI API Header Library
MSDN : gethostname, gethostbyname
함수 사용 예제
procedure
TMainForm.FormCreate(Sender: TObject);
var localhost: String;pc:PChar;ip:TStringList;begin
SetLength(localhost,260);//메모리 할당
pc := PChar( @localhost[1] );//포인터 변환
gethostname(pc,260);//현재 컴퓨터 이름 가져오기
IPList := gethostbyname(pc);//컴퓨터 이름으로부터 아이피 정보 획득
ip := TStringList.Create;
if Assigned(ip) then begin
parseIP(IPList,ip);//아이피 파싱 TStringList 에 순서대로 아이피 저장
ip.Free;
end;
SetLength(localhost,0);//메모리 해제
end;
var localhost: String;pc:PChar;ip:TStringList;begin
SetLength(localhost,260);//메모리 할당
pc := PChar( @localhost[1] );//포인터 변환
gethostname(pc,260);//현재 컴퓨터 이름 가져오기
IPList := gethostbyname(pc);//컴퓨터 이름으로부터 아이피 정보 획득
ip := TStringList.Create;
if Assigned(ip) then begin
parseIP(IPList,ip);//아이피 파싱 TStringList 에 순서대로 아이피 저장
ip.Free;
end;
SetLength(localhost,0);//메모리 해제
end;
아이피 목록 파싱 함수
function
parseIP(addrList:PHostEnt;var
ip:TStringList):Integer;
var i:Cardinal;addr:TInAddr;ipBlock:array of PChar;text:PAnsiChar;begin
Result := 0;
i := 0;
ipBlock := Pointer(addrList^.h_addr_list);
while( ipBlock[i] <> nil)do begin
addr.S_addr := PCardinal( ipBlock[i] )^;
text := inet_ntoa(addr);
if text <> nil then begin
ip.Add(text);
end;
i := i +1;
end;
Result := Result -1;
end;
var i:Cardinal;addr:TInAddr;ipBlock:array of PChar;text:PAnsiChar;begin
Result := 0;
i := 0;
ipBlock := Pointer(addrList^.h_addr_list);
while( ipBlock[i] <> nil)do begin
addr.S_addr := PCardinal( ipBlock[i] )^;
text := inet_ntoa(addr);
if text <> nil then begin
ip.Add(text);
end;
i := i +1;
end;
Result := Result -1;
end;