A
aus
Unregistered / Unconfirmed
GUEST, unregistred user!
我在程序的主窗体里面获取本机IP,并且用tidtcpserver监听本地的9091端口,结果获取本机Ip的代码如果放在开启监听之前,会报错:cannot allocate socket,如果获取本机IP的代码放在开启监听之后,不会报错,但是接收不到发送到9091的信息。Delphi7,Indy9.0获取本机Ip的代码是:function GetLocalIP: String;type TaPInAddr = array [0..10] of PInAddr; PaPInAddr = ^TaPInAddr;var i: Integer; WSADATA: TWSADATA; Buffer: array [0..63] of Char; HostEnt: PHostEnt; pptr: PaPInAddr;begin //获取本机IP WSAStartup($101, WSADATA); gethostname(Buffer, Sizeof(Buffer)); HostEnt := gethostbyname(Buffer); if Assigned(HostEnt) then begin pptr := PaPInAddr(HostEnt^.h_address_list); i := 0; while Assigned(pptr^) do begin Result := StrPas(inet_ntoa(pptr^^)); Inc(i); end; end; WSACleanup;end;请大家指教。