从客户端发送一个信息到服务器,表示查询服务器的IP和计算机名
触发事件服务器端的Button1Click
procedure TForm1.Button1Click(Sender: TObject);
var
Ip:string;
Ipstr:string;
Buffer:Array[1..32] of char;
i:integer;
WSData:TWSAdata;
Host:PHostEnt;
begin
if WSAstartup(2,WSData)<>0 then
begin
ShowMessage('WS2_32.DLL初始化失败!');
halt;
end;
try
if GetHostName(@Buffer[1],32)<>0 then
begin
ShowMessage('没有得到主机名!');
halt;
end;
except
ShowMessage('没有成功返回主机名');
halt;
end;
Host := GetHostByName(@Buffer[1]);
if Host = nil then
begin
ShowMessage('IP地址为空');
halt;
end
else
begin
Edit2.Text := Host.h_name;
for i:=1 to 4 do
begin
Ip:=IntToStr(Ord(Host.h_addr^[i-1]));
if i<4 then
Ipstr := Ipstr + Ip + '.'
else
Edit1.Text:=Ipstr+Ip;
end;
end;
WSACleanup;
end;
end.
得到结果后再发送到客户端就可以的哦