Winsock, weird sideeffect

This is a discussion on Winsock, weird sideeffect within the Networking/Device Communication forums, part of the General Programming Boards category; I got this odd error where if I send some data from a client to a server the message is ...

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    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.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #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.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #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);

  9. #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.

  10. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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