Advertisement

WSARecv Error :: Winsock

Started by May 15, 2002 03:32 PM
10 comments, last by kuphryn 22 years, 8 months ago
Hi. I am in the debugging phase of a simple message program using Winsock API. The program has both server and client features. The user can choose to be a server or a client. Anyways, everything works including server start, stop, accept client, and send data. However, I am having problems receiving data. WSARecv fail with the error WSAEFAULT. ----- // WSAEFAULT: "Bad address" ----- I have tested both sides. I get the same error no when I try to receive data from the server end and from the client end. Again, sending the data works fine, its just that I cannot receive the data. Here is the function to read the data. ----- if (socket != INVALID_SOCKET) { DWORD bufferSize = 0, receivedSize = 0; WSABUF bufferHDR, bufferDATA; char *header = new char[4], *data; bufferHDR.len = 4; bufferHDR.buf = header; // The program never makes it pass this point if (WSARecv(socket, &bufferHDR, 1, &receivedSize, 0, 0, 0) == 0) { bufferSize = static_cast(*bufferHDR.buf); data = new char[bufferSize]; bufferDATA.len = bufferSize; bufferDATA.buf = data; receivedSize = 0; DWORD bufferProgress = 0; while (receivedSize < bufferSize) { if (WSARecv(socket, &bufferDATA, 1, &bufferProgress, 0, 0, 0) == 0) { newData += bufferDATA.buf; receivedSize += bufferProgress; } else { DetermineErrorWSARecv(); receivedSize = bufferSize; } } UpdateAllViews(NULL); } else DetermineErrorWSARecv(); delete [] data; delete [] header; } ----- Have you experienced a similar problem? Thanks, Kuphryn [edited by - kuphryn on May 15, 2002 4:42:01 PM]
According to MSDN:

quote:

WSAEFAULT: The lpBuffers parameter is not completely contained in a valid part of the user address space.



Thats all it lists for WSARecv. I don''t believe it''s telling the truth

Is it possible that passing NULL to the lpdwFlags argument (5th one) is causing it to fail?
Advertisement
Oh oh. Thanks.

I am not sure what the lpbuffer is used for. What should I pass into that parameter?

Kuphryn
Okay. I believe I know one solution. The fifth parameter requires MSG_PEEK, MSG_OOB, or MSG_PARTIAL.

I will use MSG_OOB.

Kuphryn
The prototype:

int WSARecv(  SOCKET s,  LPWSABUF lpBuffers,  DWORD dwBufferCount,  LPDWORD lpNumberOfBytesRecvd,  LPDWORD lpFlags,  LPWSAOVERLAPPED lpOverlapped,  LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine); 


So you are ok up to lpFlags (lpBuffers is an array of WSABUF structures, you''ve just got a single element array which is fine). The bit which concerned me about your code is passing 0 for lpFlags, try just passing the address of some int to that and see if it stops failing. I imagine the last two parameters are ok to pass as NULL.

I got this from here.
Thanks.

I tried to pass ni MSG_OOB, but I believe WSARecv wants a LPDWORD. I am not sure about how to convert MSG_OOB to an LPDWORD variable.

Kuphryn
Advertisement
Okay. The solution to set declare a DWORD and pass that variable into the function (referece).

-----
// DWORD flags = 0;
// WSARevc(socket, &bufferHDR, 1, &receivedSize, flags, 0, 0);
-----

Kuphryn
You shouldn''t be trying to pass MSG_OOB to the flags parameter.
Glad you solved your problem.

Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Thanks.

For DWORD lpflags, wSASend required one of three flags:

MSG_PEEK
MSG_PARTIAL
MSG_OOB

I have to pass in one or more of the above flags.

Kuphryn
Um...no you don''t

Try using 0. I''d say that about 95% of the time you don''t pass anything other than 0 in the flags parameter when using WSASend.



Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com

This topic is closed to new replies.

Advertisement