C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 05-02-2005, 11:13 AM   #1
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
Winsock, weird sideeffect

I got this odd error where if I send some data from a client to a server the message is received but I also get a number of empty messages, the same amount as the nr of letters in the message (in the example below, the string "Hello" gets 5 empty messages.
It's a non-blocking system, the client sends simply using send() and the server's recieve-part looks like this:
Code:
case FD_READ:
{
	CHAR Char;
	INT Result;
	std::string String;
	std::vector<CHAR> Buffer;

	while(TRUE)
	{
		Result = recv(W, &Char, 1, 0);
				
		if(Result == SOCKET_ERROR)
		{
			if(WSAGetLastError() == WSAEWOULDBLOCK) break;
			ErrorMessage = "recv() failed!";
			return FALSE;
		}

		if(Result < 1) break;

		Buffer.push_back(Char);
	}

	Buffer.push_back('\0');
	String = "MSG: ";
	String += &Buffer[0];
	AddMessage(String);

	return TRUE;
}
As an example, if the client sends the string "Hello" to the server it would receive these messages:
Code:
MSG: Hello
MSG:
MSG:
MSG:
MSG:
MSG:
Prolly some stupid error, but I just can't find it...
__________________
MagosX.com

Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Magos is offline   Reply With Quote
Old 05-02-2005, 11:30 AM   #2
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
It seems to me you add a string even if your while loop exits on the first pass.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 05-02-2005, 11:47 AM   #3
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
I could add:
Code:
if(String != "") AddMessage(String);
or some other test to prevent this, but that doesn't remove the fact that the FD_READ message is sent 1000 times for a 1000-byte message, which is insane...
__________________
MagosX.com

Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Magos is offline   Reply With Quote
Old 05-02-2005, 12:25 PM   #4
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
Are you saying that you read a char, then the loop breaks out because of this
if(WSAGetLastError() == WSAEWOULDBLOCK) break;
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 05-02-2005, 12:25 PM   #5
Registered User
 
Join Date: Jan 2005
Posts: 847
Quote:
Originally Posted by Magos
that doesn't remove the fact that the FD_READ message is sent 1000 times for a 1000-byte message, which is insane...
That's because you read it char at a time and FD_READ is sent to indicate that data is still available.
Quantum1024 is offline   Reply With Quote
Old 05-02-2005, 12:37 PM   #6
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
Quote:
That's because you read it char at a time and FD_READ is sent to indicate that data is still available.
That seems like a logical reason for my "error". Would it be better to read it it chunks (say 1024 bytes) and then check by the return value how much of the buffer is valid and use that?
__________________
MagosX.com

Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Magos is offline   Reply With Quote
Old 05-02-2005, 12:40 PM   #7
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
Is there some specific API you can use to find the amount of data available at the moment?

At the moment, you can only guarantee with 'data available' that there is at least one byte.

If you try and read 10 say, and there is only 9 available, then I think you're going to get "WOULDBLOCK" back straight away.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 05-02-2005, 12:48 PM   #8
Registered User
 
Join Date: May 2005
Posts: 24
you can (I think) query the recieve buffer with something like this:

needed = recv(W, &Char, 0, MSG_PEEK);
legend is offline   Reply With Quote
Old 05-02-2005, 01:13 PM   #9
Registered User
 
Join Date: Jan 2005
Posts: 847
MSG_PEEK fills the buffer just the same the only difference is that the data isn't removed from wnsock's internal bufers so another recv call would return the same data. I don't recomend using MSG_PEEK EVER!
To get the amount of data available use ioctlsocket with the FIONREAD flag.
The 3rd argument to recv isn't the amount of data you want to receive it is the length of the buffer where the data will be placed so if there is less data then the buffer length recv will still return with no error, the amount received is the return value.
Quantum1024 is offline   Reply With Quote
Old 05-02-2005, 01:46 PM   #10
Registered User
 
Join Date: May 2005
Posts: 24
I was basing my advice on this from MSDN:

"MSG_PEEK: Peeks at the incoming data. The data is copied into the buffer, but is not removed from the input queue. The function subsequently returns the amount of data that can be read in a single call to the recv."

but on closer inspection:

"...which may not be the same as the total amount of data queued on the socket".

so that probably wouldn't be such a good idea after all.
legend is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Winsock issues tjpanda Windows Programming 3 12-04-2008 08:32 AM
Winsock Messaging Program Morgul Windows Programming 13 04-25-2005 04:00 PM
Where do I initialize Winsock and catch messages for it? Lithorien Windows Programming 10 12-30-2004 12:11 PM
weird winsock output and winsock 2 not working whackaxe Networking/Device Communication 4 07-09-2004 11:10 AM
winsock pode Networking/Device Communication 2 09-26-2003 12:45 AM


All times are GMT -6. The time now is 01:25 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22