2011/06/08

Get IPv4 address from sockaddr or socket

(sock_fd is a socket)
sockaddr_in  addr_in;
int          len = sizeof(sockaddr_in);

/* Get sockaddr_in structure information from socket */
getsockname(sock_fd, (sockaddr *)&addr_in, (socklen_t *)&len);

/* Get address from sockaddr_in */
printf("IP=[%s] port=[%d]\n", inet_ntoa(addr_in.sin_addr), addr_in.sin_port);


沒有留言:

張貼留言