I'd like to have my server determine the source IP and port of a client from a connected TCP socket. Since my clients are likely behind NAT's, I can't rely on being told by the client (in the protocol of the connection)... If this is possible, I'm going to need to implement it on both Windows and Linux... But an answer for either would help get me started...
I am using C, and I'm looking for either libc or msvcrt based solutions.
Should work both in linux and windows:
struct sockaddr_in addr;
socklen_t len;
len = sizeof addr;
getpeername(clientSocket, (struct sockaddr*)&addr, &len);
printf("Remote IP address: %s\n", inet_ntoa(addr.sin_addr));