Sign in to follow this  

WSARecvFrom causes WSAEFAULT error

This topic is 4206 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I'm programming an application using UDP and Winsock 2 (asynchronous, but non-overlapped sockets). I've got a problem receiving packets, though... the handling looks like this:
case FD_READ:
  DWORD numberOfBytesReceived;
  sockaddr_in address;
  int sockaddrSize = sizeof(sockaddr);
  int fail = WSARecvFrom(socket, &readBuffer, 1, &numberOfBytesReceived, 0, (sockaddr*) &address, &sockaddrSize, NULL, NULL);

where 'socket' is a socket that has been bound, 'readBuffer' is a WSABUF (and I'm using that casting from sockaddr_in to sockaddr because beej's tutorial told me to). When I send a datagram to the application, I get to the last line, which returns an error. This is defined as: WSAEFAULT The lpBuffers, lpFlags, lpFrom, lpNumberOfBytesRecvd, lpFromlen, lpOverlapped, or lpCompletionRoutine parameter is not totally contained in a valid part of the user address space: the lpFrom buffer was too small to accommodate the peer address. how can the lpFrom parameter ('(sockaddr*) &address') be too small? I'm thankful for any insights into this problem :) Dr. Lecter

Share this post


Link to post
Share on other sites
The line lists all possible things that could cause that error. Chances are, it's something other than the struct sockaddr_in.

If you're going to pass NULL for overlapped/completion, you might as well use regular ::recvfrom() by the way, and make your code more portable.

Last: the lpFlags parameter isn't documented as accepting NULL on MSDN -- try passing a pointer to a DWORD containing 0 instead.

Share this post


Link to post
Share on other sites
int sockaddrSize = sizeof(sockaddr);
Should be:
int sockaddrSize = sizeof(sockaddr_in);
Although I don't know if the structures are actually different sizes...

Share this post


Link to post
Share on other sites

This topic is 4206 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this