Q&A

  • LAN으로 연결된 다른 컴퓨터 IP를구하고 싶어요.
델초보입니다.

LAN으로 상대방 컴퓨터의이름과 IP를 구해야 하는데 팁을 가지고 짜집기했는데

잘 안되네요.

아래 소스는 실행은 되지만 선택된 컴퓨터의 IP가 Label1.caption에

나타나야 되는데 안됩니다.

폼에는 버튼과 라벨이 하나씩 있고 uses절에 shlobj를 사용했습니다.)

저는 이걸로 급히 수행해야 되는 일이 있는데

정말 미치겠습니다. 제발 도와주세요. 진짜 급합니다. 흑흑..



unit msg1;



interface



uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

StdCtrls,shlobj,Winsock;



type

TForm1 = class(TForm)

Button1: TButton;

Label1: TLabel;

procedure Button1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

function GetNetworkComputer:String;

end;



var

Form1: TForm1;



implementation

{function LocalIP: string;

type

TaPInAddr = array [0..10] of PInAddr;

PaPInAddr = ^TaPInAddr;

var

phe: PHostEnt; // HostEntry 구조체

pptr: PaPInAddr;

Buffer: array [0..MAX_PATH] of char;

i: Integer;

begin

LocalIP := '';

// GetHostName()은 지금 호스트(PC)의 호스트 이름을 Buffer에 얻어낸다

// 호스트 이름은 0으로 끝나는 문자열이 된다

GetHostName(Buffer, SizeOf(Buffer));



// GetHostByName 함수는 인자에 해당하는 호스트의 이름을 가진 호스트에 대한

// hostent구조에 대한 포인터를 리턴한다.

// GetHostByName 함수는 보통 네트워크 데이터베이스의 hosts파일을 참조하거나,

// 도메인 네임 서버(domain name server)에다 주소에 해당하는 호스트를 찾는

// 메시지를 보내고 그에 대한 응답을 기다린다.

phe := GetHostByName(buffer); // Client의 IP이므로 비동기하지 않았음

if phe = nil then

begin

LocalIP := '127.0.0.1';

System.Exit;

end;



pptr := PaPInAddr(Phe^.h_addr_list);

i := 0;

while pptr^[i] <> nil do // Multi-Homed 일 경우는 여러개의 IP가 있을 수 있으므로

begin

LocalIP := StrPas(inet_ntoa(pptr^[i]^));

Inc(i);

end;

end;}



function TForm1.GetNetworkComputer:string;

var

PIDL : PItemIDList;

DisplayName : array[0..MAX_PATH] of Char;

BrowseInfo : TBrowseInfo;

begin



{ Get the PItemIDList for CSIDL_NETWORK }

SHGetSpecialFolderLocation(Handle,

CSIDL_NETWORK,

PIDL);



{ Fill our TBrowseInfo record }

BrowseInfo.hwndOwner := Handle;

BrowseInfo.pidlRoot := PIDL;

BrowseInfo.pszDisplayName := @DisplayName[0];

BrowseInfo.lpszTitle := 'Select Network Computer';

BrowseInfo.ulFlags := BIF_BROWSEFORCOMPUTER;

BrowseInfo.lpfn := nil;



{ Display dialogbox according to our TBrowseInfo }

SHBrowseForFolder(BrowseInfo);



{ Return our network computer name }

Result := DisplayName;

end;



{...}



{$R *.DFM}



procedure TForm1.Button1Click(Sender: TObject);

type

TaPInAddr = array [0..10] of PInAddr;

PaPInAddr = ^TaPInAddr;

var

phe: PHostEnt; // HostEntry 구조체

pptr: PaPInAddr;

Buffer:array [0..MAX_PATH] of char;

i: Integer;

LocalIP:string;

tmp:string;

begin

LocalIP := '';



showmessage(GetNetworkComputer());

for i:=1 to length(tmp) do

buffer[i-1]:=tmp[i];

GetHostName(buffer, SizeOf(buffer));

phe := GetHostByName(buffer); // Client의 IP이므로 비동기하지 않았음

if phe = nil then

begin

LocalIP := '127.0.0.1';

System.Exit;

end;



pptr := PaPInAddr(Phe^.h_addr_list);

i := 0;

while pptr^[i] <> nil do // Multi-Homed 일 경우는 여러개의 IP가 있을 수 있으므로

begin

LocalIP := StrPas(inet_ntoa(pptr^[i]^));

Inc(i);

end;

//showmessage(GetNetworkComputer());

label1.caption:=LocalIP;

end;



end.

2  COMMENTS
  • Profile
    김영대 1999.06.24 18:47
    장일형 께서 말씀하시기를...

    > 델초보입니다.

    > LAN으로 상대방 컴퓨터의이름과 IP를 구해야 하는데 팁을 가지고 짜집기했는데

    > 잘 안되네요.

    > 아래 소스는 실행은 되지만 선택된 컴퓨터의 IP가 Label1.caption에

    > 나타나야 되는데 안됩니다.

    > 폼에는 버튼과 라벨이 하나씩 있고 uses절에 shlobj를 사용했습니다.)

    > 저는 이걸로 급히 수행해야 되는 일이 있는데

    > 정말 미치겠습니다. 제발 도와주세요. 진짜 급합니다. 흑흑..

    >

    > unit msg1;

    >

    > interface

    >

    > uses

    > Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

    > StdCtrls,shlobj,Winsock;

    >

    > type

    > TForm1 = class(TForm)

    > Button1: TButton;

    > Label1: TLabel;

    > procedure Button1Click(Sender: TObject);

    > private

    > { Private declarations }

    > public

    > { Public declarations }

    > function GetNetworkComputer:String;

    > end;

    >

    > var

    > Form1: TForm1;

    >

    > implementation

    > {function LocalIP: string;

    > type

    > TaPInAddr = array [0..10] of PInAddr;

    > PaPInAddr = ^TaPInAddr;

    > var

    > phe: PHostEnt; // HostEntry 구조체

    > pptr: PaPInAddr;

    > Buffer: array [0..MAX_PATH] of char;

    > i: Integer;

    > begin

    > LocalIP := '';

    > // GetHostName()은 지금 호스트(PC)의 호스트 이름을 Buffer에 얻어낸다

    > // 호스트 이름은 0으로 끝나는 문자열이 된다

    > GetHostName(Buffer, SizeOf(Buffer));

    >

    > // GetHostByName 함수는 인자에 해당하는 호스트의 이름을 가진 호스트에 대한

    > // hostent구조에 대한 포인터를 리턴한다.

    > // GetHostByName 함수는 보통 네트워크 데이터베이스의 hosts파일을 참조하거나,

    > // 도메인 네임 서버(domain name server)에다 주소에 해당하는 호스트를 찾는

    > // 메시지를 보내고 그에 대한 응답을 기다린다.

    > phe := GetHostByName(buffer); // Client의 IP이므로 비동기하지 않았음

    > if phe = nil then

    > begin

    > LocalIP := '127.0.0.1';

    > System.Exit;

    > end;

    >

    > pptr := PaPInAddr(Phe^.h_addr_list);

    > i := 0;

    > while pptr^[i] <> nil do // Multi-Homed 일 경우는 여러개의 IP가 있을 수 있으므로

    > begin

    > LocalIP := StrPas(inet_ntoa(pptr^[i]^));

    > Inc(i);

    > end;

    > end;}

    >

    > function TForm1.GetNetworkComputer:string;

    > var

    > PIDL : PItemIDList;

    > DisplayName : array[0..MAX_PATH] of Char;

    > BrowseInfo : TBrowseInfo;

    > begin

    >

    > { Get the PItemIDList for CSIDL_NETWORK }

    > SHGetSpecialFolderLocation(Handle,

    > CSIDL_NETWORK,

    > PIDL);

    >

    > { Fill our TBrowseInfo record }

    > BrowseInfo.hwndOwner := Handle;

    > BrowseInfo.pidlRoot := PIDL;

    > BrowseInfo.pszDisplayName := @DisplayName[0];

    > BrowseInfo.lpszTitle := 'Select Network Computer';

    > BrowseInfo.ulFlags := BIF_BROWSEFORCOMPUTER;

    > BrowseInfo.lpfn := nil;

    >

    > { Display dialogbox according to our TBrowseInfo }

    > SHBrowseForFolder(BrowseInfo);

    >

    > { Return our network computer name }

    > Result := DisplayName;

    > end;

    >

    > {...}

    >

    > {$R *.DFM}

    >

    > procedure TForm1.Button1Click(Sender: TObject);

    > type

    > TaPInAddr = array [0..10] of PInAddr;

    > PaPInAddr = ^TaPInAddr;

    > var

    > phe: PHostEnt; // HostEntry 구조체

    > pptr: PaPInAddr;

    > Buffer:array [0..MAX_PATH] of char;

    > i: Integer;

    > LocalIP:string;

    > tmp:string;

    > begin

    > LocalIP := '';

    >

    > showmessage(GetNetworkComputer());

    > for i:=1 to length(tmp) do

    > buffer[i-1]:=tmp[i];

    > GetHostName(buffer, SizeOf(buffer));

    > phe := GetHostByName(buffer); // Client의 IP이므로 비동기하지 않았음

    > if phe = nil then

    > begin

    > LocalIP := '127.0.0.1';

    > System.Exit;

    > end;

    >

    > pptr := PaPInAddr(Phe^.h_addr_list);

    > i := 0;

    > while pptr^[i] <> nil do // Multi-Homed 일 경우는 여러개의 IP가 있을 수 있으므로

    > begin

    > LocalIP := StrPas(inet_ntoa(pptr^[i]^));

    > Inc(i);

    > end;

    > //showmessage(GetNetworkComputer());

    > label1.caption:=LocalIP;

    > end;

    >

    > end.



    안녕하세요 장일형님...



    제 홈페이지(http://myhome.shinbiro.com/~cozykyd/index.htm)의 첫 화면에

    있는 "공개 프로그램 소스" 에 보시면

    "[공개] 상대방 컴퓨터 이름으로 IP주소 구하기 (반대기능도 가능)" 이 있는데

    참고해 보세요

  • Profile
    장일형 1999.06.24 20:04
    김영대님

    많은 도움이 되었습니다. 그런데 테스트해보니

    같은 작업그룹은 잘 되는데

    다른 작업 그룹은 IP를 찾지 못하네요. 방법이 없을까요?