如何获得本机的IP地址,在点对点通信程序中如何获得发消息过来的主机的IP地址

smuwcwt 2003-12-17 05:47:58
RT
...全文
94 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyq 2004-01-27
  • 打赏
  • 举报
回复
idudpserver
smuwcwt 2004-01-27
  • 打赏
  • 举报
回复
好的,先试一下
fontain 2004-01-26
  • 打赏
  • 举报
回复
把下面的这个包装上,在borland\delphi7\bin下面
dclsockets70.bpl(Borland Socket Components)
包括的控件:TClientSocket、TServerSocket
smuwcwt 2004-01-24
  • 打赏
  • 举报
回复
UP
smuwcwt 2004-01-23
  • 打赏
  • 举报
回复
真的没通过呀
smuwcwt 2004-01-23
  • 打赏
  • 举报
回复
UP
smuwcwt 2004-01-22
  • 打赏
  • 举报
回复
Delphi7.0好象通不过呀
月光 2004-01-22
  • 打赏
  • 举报
回复
楼主,你连udp还是tcp/ip都不清楚吗?
gdwyh 2004-01-20
  • 打赏
  • 举报
回复
获得本机IP:
function tloginfrm.getip:string;
//返回IP
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;
begin
// returns ISP assigned IPf

WSAStartup($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 begin
result:=StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;
end;
smuwcwt 2004-01-20
  • 打赏
  • 举报
回复
up
Wyi23 2004-01-20
  • 打赏
  • 举报
回复
本机IP
uses
...
WinSock,
...

function LocalIP:string;
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;
begin
WSAStartup($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 begin
result:=StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;
end;
在Delphi6可以通过
smuwcwt 2004-01-20
  • 打赏
  • 举报
回复
????楼上的兄弟说清楚点好吗?
月光 2004-01-20
  • 打赏
  • 举报
回复
看看delphi自带的demo,udp就看udp,tcp/ip的我不记得在哪个目录,找找
SuperFatcow 2004-01-19
  • 打赏
  • 举报
回复
我试了一下,好象确实不行,楼上的楼上讲清楚点啊
smuwcwt 2003-12-26
  • 打赏
  • 举报
回复
真的不行呀
2312 2003-12-18
  • 打赏
  • 举报
回复
把客户端IP一块发到服务器,然后进行分解!

获得本机IP:
procedure TF_main.getipaddress; //»ñµÃ±¾»úIPµØÖ·
var ip,ipstr:string;
ch:array[1..32] of char;
i:integer;
wsdata:twsadata;
myhost:phostent;
begin
if wsastartup(2,wsdata)<>0
then
begin
messagedlg('ûÓгɹ¦·µ»Ø£¡',mtinformation,[mbok],0);
halt(2);
end;
try
if gethostname(@ch[1],32)<>0 then
begin
messagedlg('ûÓгɹ¦·µ»Ø£¡',mtinformation,[mbok],0);
halt(3);
end;
except
messagedlg('ûÓгɹ¦·µ»Ø£¡',mtinformation,[mbok],0);
halt(3);
end;
myhost:=gethostbyname(@ch[1]);
if myhost=nil then
begin
messagedlg('ipµØÖ·Îª¿Õ',mtinformation,[mbok],0);
halt(4);
end
else
begin
for i:=1 to 4 do
begin
ip:=inttostr(ord(myhost.h_addr^[i-1]));
ipstr:=ipstr+ip;
if i<4 then
ipstr:=ipstr+'.'
else
strIP:=ipstr;
end;
end;
end;

你可以写成函数
crapex 2003-12-18
  • 打赏
  • 举报
回复
D7下面没有ServerSocket的,后面那个不行,但前面那个可以
是需要uses winsock
smuwcwt 2003-12-18
  • 打赏
  • 举报
回复
好像在DELPHI 7下都不能运行呀
quansui 2003-12-17
  • 打赏
  • 举报
回复
如果你不想编太多的代码的话用powersock控件吧。很方便获得本机IP
x:=Powersock1.LocalIP;
对方的IP:如果你使用的是WinSocket在这个事件中得到
procedure TSocketData.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
ClientIP:=Socket.RemoteAddress;
end;
crapex 2003-12-17
  • 打赏
  • 举报
回复
1.本机IP
function LocalIP:string;
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;
begin
WSAStartup($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 begin
result:=StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;
end;
2.对方IP
ServerSocket1.Socket.Connections[i].RemoteAddress

1,594

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧