Advertisement

Odd error with WSARecv: WSAEFAULT

Started by June 24, 2003 06:50 PM
5 comments, last by QBRADQ 20 years, 8 months ago
Hey there Game Devers! It''s been a while! Anyhow, down to some business. It has taken me two long weeks of learning, implementing, coding a demo, and repeatting the process. I have learned a great deal about network programming, but now this comes up. Every time I call WSARecv it kicks the error WSAEFAULT. From the SDK Docs: The lpBuffers parameter is not completely contained in a valid part of the user address space. From the MSDN: Bad address. The system detected an invalid pointer address in attempting to use a pointer argument of a call. This error occurs if an application passes an invalid pointer value, or if the length of the buffer is too small. For instance, if the length of an argument, which is a sockaddr structure, is smaller than the sizeof(sockaddr). And the bit of relevant code: WSABUF buf; char cTemp[128]; buf.buf = cTemp; buf.len = 128; // Pass WSARecv &buf, number of buffers 1 If I understood what the error message was saying, it might help. I have tried fooling with size of the buffers, the length arguments, putting them in global scope, thread scope, function scope, and tried making an array of WSABUFs. Nothing works Has anyone ever ran across this before? Or perhapse you could shed some more light on those error messages? On a side note, recv works fine, but ReadFile does not. I am working under IOCP, but the those mechanisims are not yet in place ( as WSARecv has to suceed in order to push a completion packet ). Also, the socket that all of these operations are being preformed on is a UDP socket with the broadcast option on. Thanks for your help, Q
Tell him about the twinky...
Sorry to bring back an older post, but I have searched all over and couldn''t figure this one out... I''m having the same problem, but with a TCP socket (not that it should matter).

Any ideas?
Advertisement
Assuming you''re using asynchronous/overlapped operation: It looks like you''re putting the buffer on the stack. That means it''ll be gone by the time your asynchronous receive actually completes. You should point the buffer pointer at storage area that will remain valid, such as a malloc() block or a global buffer.

Also, WSARect takes an array of buffer descriptors, you have to pass in the right buffer count. Something like this might help:

quote:

char g_data[ 128 ];
WASBUF g_wsaBuf;
DWORD g_dataSize;
DWORD g_flags;

HRESULT Receive( SOCKET s, WSAOVERLAPPED wsao, WSAOVERLAPPED_COMPLETION_ROUTINE wsaocr ) {
memset( &g_wsaBuf, 0, sizeof( g_wsaBuf ) );
g_wsaBuf.len = 128;
g_wsaBuf.ptr = g_data;
g_dataSize = 0;
g_flags = 0;
return WSARecv( s, &g_wsaBuf, 1, &g_dataSize, &g_flags, wsao, wsaocr );
}


enum Bool { True, False, FileNotFound };
I have tried putting the buffers in global scope, just like the OP did.

Just to make sure, I tried again, running the code you provided (which was basically the exact same as what I had) and WSARecv still fails with error code 10014 (WSAEFAULT).

By the way, I appreciate all the help hplus... Hacking through this IOCP stuff is quite a chore for me, glad to see somebody willing to help that knows about the subject.

Any other thoughts?
> I am working under IOCP {...}
> {...} all of these operations are being preformed
> on is a UDP socket {...}

http://pages.infinit.net/cbenoi1/iocp_udp.zip

-cb
There''s an overlapped sockets example at the bottom of the MSDN description for WSARecv(). Can you compile and run this? Does it work?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/wsarecv_2.asp
enum Bool { True, False, FileNotFound };
Advertisement
Problem solved... My invalid pointer was the flags variable, nothing to do with the buffers. Stupid mistakes, as usual...

Thanks for the help.

This topic is closed to new replies.

Advertisement