SDLNet_TCP_GetPeerAddress

Name

SDLNet_TCP_GetPeerAddress -- get the address of the remote end of a socket.

Synopsis

IPaddress *SDLNet_TCP_GetPeerAddress(TCPsocket sock)

sock is a connected TCP socket.

Description

SDLNet_TCP_GetPeerAddress returns the address of the peer's end of the socket. This contains the peer's IP address and the TCP port number of that end of the connection. This function is commonly used in servers after a call to SDLNet_TCP_Accept. A server would use this function to get the address of a new client who just connected.

Return Value

Returns a pointer to an IPaddress structure which contains the host and port fields, in network byte order. This pointer is valid until the socket is closed with SDLNet_TCP_Close.

Returns NULL if the socket is a server socket (used for listening).

Example

//TCPsocket new_tcpsock;
IPaddress *remote_ip;

remote_ip = SDLNet_TCP_GetPeerAddress(new_tcpsock);
if(!remote_ip) {
    printf("SDLNet_TCP_GetPeerAddress: %s\n", SDLNet_GetError());
    printf("This may be a server socket.\n");
}
else {
    // print the info in IPaddress or something else...
}

See Also

SDLNet_TCP_Open, SDLNet_TCP_Accept, IPaddress, TCPsocket

SDLNet_TCP_GetPeerAddress (last edited 2008-04-17 08:18:57 by localhost)