function TFrmChat.HostName: String; var Buf: Array[0..255] of char; GInitData: TWSADATA; wVerReq: Word; begin wVerReq := MakeWord(2, 0); WSAStartup(wVerReq, GInitData); Result := ''; GetHostName(Buf, Sizeof(Buf)); Result := StrPas(Buf); WSACleanup; end;
用次函数就可以完成计算机名到IP地址的转换function nametoip(name:string):string; var couputername:string; hostent:phostent; begin couputername:=edit1.Text; hostent:=gethostbyname(pchar(couputername)); if hostent<>nil then begin with hostent^ do result:=format('%d.%d.%d.%d',[byte(h_addr^[0]),byte(h_addr^[1]),byte(h_addr^[2]),byte(h_addr^[3])]); end; end;
function LocalIP:string; typeTaPInAddr = array [0..10] of PInAddr;PaPInAddr = ^TaPInAddr;varphe : PHostEnt;pptr : PaPInAddr;Buffer : array [0..63] of char;I : Integer;GInitData : TWSADATA;beginWSAStartup($101, GInitData);Result := '';GetHostName(Buffer, SizeOf(Buffer));phe :=GetHostByName(buffer);if phe = nil then Exit;pptr := PaPInAddr(Phe^.h_addr_list);I := 0;while pptr^[I] <> nil do beginresult:=StrPas(inet_ntoa(pptr^[I]^));Inc(I);end;WSACleanup;end;
var
Buf: Array[0..255] of char;
GInitData: TWSADATA;
wVerReq: Word;
begin
wVerReq := MakeWord(2, 0);
WSAStartup(wVerReq, GInitData);
Result := '';
GetHostName(Buf, Sizeof(Buf));
Result := StrPas(Buf);
WSACleanup;
end;
这样能够让通过服务器连上网的,不同电脑建立点对点连接吗?就像QQ中一样?
看来只有通过服务器转一种方法了。
var
couputername:string;
hostent:phostent;
begin
couputername:=edit1.Text;
hostent:=gethostbyname(pchar(couputername));
if hostent<>nil then
begin
with hostent^ do
result:=format('%d.%d.%d.%d',[byte(h_addr^[0]),byte(h_addr^[1]),byte(h_addr^[2]),byte(h_addr^[3])]);
end;
end;
typeTaPInAddr = array [0..10] of PInAddr;PaPInAddr = ^TaPInAddr;varphe : PHostEnt;pptr : PaPInAddr;Buffer : array [0..63] of char;I : Integer;GInitData : TWSADATA;beginWSAStartup($101, GInitData);Result := '';GetHostName(Buffer, SizeOf(Buffer));phe :=GetHostByName(buffer);if phe = nil then Exit;pptr := PaPInAddr(Phe^.h_addr_list);I := 0;while pptr^[I] <> nil do beginresult:=StrPas(inet_ntoa(pptr^[I]^));Inc(I);end;WSACleanup;end;