开发者

How to check if a socket client-server is on same network?

开发者 https://www.devze.com 2023-01-09 15:37 出处:网络
I\'m working with socket and to this I\'m using TIdTCPClient and TIdTCPSe开发者_如何学Gorver. I need to check if the TIdTCPServer that the TIdTCPClient connected is on the same network.

I'm working with socket and to this I'm using TIdTCPClient and TIdTCPSe开发者_如何学Gorver. I need to check if the TIdTCPServer that the TIdTCPClient connected is on the same network.

How can I do this ?

at.


You need to know the client's subnet mask in order to do that kind of comparison. Sockets do not expose that information, so you will have to ask the OS directly (for instance, on Windows, you can look for the client's connected local IP in the list returned by GetAdaptersInfo() or GetAdapterAddresses()). Once you have the mask, you can then mask the client's IP and the server's IP with it and see if the resulting values are the same.


What do you mean for "same network"? You could mimic the traceroute utility and check how many hops (with their addresses of routers) are, and compare with the expected ones.


Tks for hits, to solve my case I just needed to verify if the host is a local host or not.

The solution:

function IsLocalHost(AHost : string) : Boolean;
var
  LStrRegexRedeLocal : string;
begin
  if LowerCase(AHost) = 'localhost' then
    result := True
  else
  begin
    LStrRegexRedeLocal := '(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)|(^127\.0\.0\.1)';
    result := ExecRegExpr(LStrRegexRedeLocal, AHost);
  end;
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号