如何sockaddr_in
从套接字句柄获取关联,以便从中获取端口和地址?或者还有其他关于如何从 SocketHandle 获取 IP 和端口的信息吗?
例如:
function GetSocketPort ( s : TSocket ) : Integer;
var
Addr : sockaddr_in;
begin
// Get sockaddr_in from a socket
end;
function GetSocketAddress ( s : TSocket ) : String;
var
Addr : sockaddr_in;
begin
// Get sockaddr_in from a socket
end;
function ConnectToHost (Host : pchar; Port : Integer) : TSocket;
var
Addr : sockaddr_in;
begin
Addr.sin_family := AF_INET;
Addr.sin_port := htons(Port);
result := Socket(AF_INET, 1, 6);
Addr.sin_addr.S_addr := INET_ADDR(Host);
if not( (Connect(result, Addr, SizeOf(Addr)) = 0) ) then result := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MySocket : TSocket;
begin
MySocket := ConnectToHost ('127.0.0.1', 8080);
if MySocket <> 0 then begin
// GetSocketAddress (MySocket);
// GetSocketPort (MySocket);
end;
end;