Rodrigo Farias Rezino
Rodrigo Farias Rezino

Reputation: 2799

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

I'm working with socket and to this I'm using TIdTCPClient and TIdTCPServer. I need to check if the TIdTCPServer that the TIdTCPClient connected is on the same network.

How can I do this ?

at.

Upvotes: 0

Views: 1560

Answers (3)

Rodrigo Farias Rezino
Rodrigo Farias Rezino

Reputation: 2799

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;

Upvotes: 0

Remy Lebeau
Remy Lebeau

Reputation: 595295

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.

Upvotes: 6

user160694
user160694

Reputation:

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.

Upvotes: 1

Related Questions