I have a problem using getsockname function. I have this code:
struct sockaddr sa;
int sa_len;
sa_len = sizeof(sa);
if (getsockname(socketfd, &sa, &sa_len) != SOCKET_ERROR)
{
///
}
else
{
int error = WSAGetLastError();
//error here WSAEFAULT always
}
As you can see, i always have error when use getsockname function. Error – WSAEFAULT. But why? structure and structure size are right, why this happens?
WSAEFAULT desc:
The name or the namelen parameter is not a valid part of the user
address space, or the namelen parameter is too small.
p.s. Application is 64 bit
Thanks!
Your
struct sockaddris too small to accept the socket address. Either use an appropriately sized struct, such asstruct sockaddr_in, or better yet, use astruct sockaddr_storage, which is guaranteed to be large enough to contain the address. Using asockaddr_storagealso allows you to easily support both IPv4 and IPv6 with minimal adjustments.Edited code: