The getsockname() function retrieves the locally-bound name of the specified socket, stores this address in the sockaddr structure pointed to by the address argument, and stores the length of this address in the object pointed to by the address_len argument.
int getsockname(int socket, struct sockaddr *address,socklen_t *address_len);