tags:

views:

221

answers:

1

How can I get the ip address of a client when he tries to connect to the server? I'm using CSocket class.

+3  A: 

void getPeer(unsigned short& port, std::string& peer);

Returns information about the remote side of the socket. port is the port on which the connection is held, and peer is the host to which the socket is connected. The peer is either a fully qualified domain name (if the IP address can be resovled via gethostbyaddr(2) or a stringified dotted IP address if not.

From here.

The getPeer function will either return the domain name or the IP address of the remote peer, depending on what it can find.

There appear to be two different definitions of CSocket depending on operating system or library. If you're using the MFC definition of CSocket then the function you want is actually getPeerName() or getPeerNameEx() if you're using IP6. From here:

BOOL GetPeerName(
   CString& rPeerAddress,
   UINT& rPeerPort 
);
BOOL GetPeerName(
   SOCKADDR* lpSockAddr,
   int* lpSockAddrLen 
);

And from here:

BOOL GetPeerNameEx(
   CString& rPeerAddress,
   UINT& rPeerPort 
);
Daniel Bingham