If connected, the function returns the IP address of the foreign host. If not, the returned IP is invalid. I know this can also be done by saving the IP of the foreign host on connection, but this can get quite hard - for example if you have an own (in my case cryptographic) session management in your server and don't have to (and don't want to) rely on a constant IP address for a client. The clients and the server are not keeping the connections alive - this is not possible for a server with possibly more than 1000 clients. You'll get in trouble updating the IP, make your storage for ips thread-safe and so on...
sf::IPAddress SocketTCP::GetConnectedAddress() const
{
if(IsValid())
{
sockaddr_in ipbuffer;
SocketHelper::LengthType len = static_cast<SocketHelper::LengthType>(sizeof(ipbuffer));
if(getpeername(mySocket,reinterpret_cast<sockaddr*>(&ipbuffer),&len) == 0)
return IPAddress(inet_ntoa(ipbuffer.sin_addr));
}
return IPAddress();
}