Page 1 of 1

Server socket caller info

PostPosted: Sat Dec 01, 2018 9:15 am
by denville
Hello friends and a very happy Christmas to you all.

When a (TCP IPv4) server socket is opened, is there a reliable, generic way to know whether the caller is on a local intranet (which may be a router or two away) or has connected via the Internet ?

Re: Server socket caller info

PostPosted: Mon Dec 03, 2018 1:09 pm
by rlebeau
denville wrote:When a (TCP IPv4) server socket is opened, is there a reliable, generic way to know whether the caller is on a local intranet (which may be a router or two away) or has connected via the Internet ?


If the client is not on the same local subnet as the server, then no. Once you cross subnet boundaries, there is no direct way to know how many subnets away the client actually is (though, a TRACERT may show you how many network segments the query has to pass through to reach the client).

To know if the client is on the same subnet as the server, obtain the client's remote IP address, and the local server IP that the client connected to (both can be gotten from the accepted socket), then obtain the local server IP's subnet mask (can be gotten from the OS, not from the accepted socket), and mask both IPs with it. If the resulting values match, the client is on the server's local subnet. If not, then the client is on another subnet, or a completely different network.

Re: Server socket caller info

PostPosted: Wed Dec 05, 2018 10:28 am
by denville
Thanks Remy