How to convert sin_addr.S_un into char *?
I need to get an IP address from a sender after he sent something to me. I'm using UDP socket, so the IP address of the sender will be automatically registered into sin_addr
structure since I'm using recvfrom()
function.
How do I mani开发者_运维技巧pulate the sin_addr
structure and convert it into a char *
?
Thank you.
inet_ntoa ?
struct sockaddr_in in_sa;
char* ip_addr_text = inet_ntoa( in_sa.sin_addr );
If you want to keep it for later use you should make a copy, for instance:
std::string ip_txt( inet_ntoa( in_sa.sin_addr ) );
// other stuff
std::cout << ip_txt << std::endl;
精彩评论