Thread: function to receive all

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    function to receive all

    Because data isn't sent all on one packet, it can be received in parts. I tried writing a function to fix this problem, but it doesn't work. Anyone know how to do this?

    Code:
    int GetFullRecv(CHAR *cMsg)
    {
    	char *tok;
    	tok = strtok(cMsg, "~");
    	if (tok == NULL)
    	{
    		if (strlen(cMsg) > 0)
    			strcat(recvbuff, cMsg);
    		return 0;
    	}
    	while (tok != NULL)
    	{
    		strcat(recvbuff, tok);
    		ZeroMemory(tmpbuff, 350);
    		strcat(tmpbuff, "RECV: ");
    		strcat(tmpbuff, recvbuff);
    		DispStatus(tmpbuff);
    		ZeroMemory(recvbuff, 300);
    		ZeroMemory(tmpbuff, 350);
    		tok = strtok(NULL, "~");
    	}
    	if (strlen(cMsg) > 0)
    		strcat(recvbuff, cMsg);
    	return 0;
    }
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    were is recvbuff,tmpbuff

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    maybe its the CHAR
    int GetFullRecv(CHAR *cMsg)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well trampling on your message using strtok(), and assuming that your message fragment has a \0 in the right place are probably two mistakes to consider
    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.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read my last post in this thread and adapt the code to suit. The readline function is probably the closest to what you're asking for.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    ok, I've looked at that function, and tried to make it what I need. Unfortunately, my programs freeze when sending/receiving data.

    I put this function in a class that has sckPeer as the open socket. I did len=sizeof(buff) (I had this in my original class recv() function and it worked.

    Code:
    INT PEER::ReadCRLF(CHAR *buff)
    {
    	char  *tmpbuf = buff;
    	int   tmpsock;
    	char  data;
    	char  lastdata = 0;
    	size_t len = sizeof(buff);
    
    	while (len > 0)
    	{
    		if ((tmpsock = recv(sckPeer, &data, 1, 0)) != 1)
    		{
    			/*
    			*  If we were interrupted, keep going,
    			*  otherwise, return EOF or the error.
    			*/
    			if (tmpsock < 0)
    				continue;
    			return(tmpsock);
    		}
            
    		if (data == '\n')
    		{
    			if (lastdata == '\r')
    				buff--;
    			*buff = '\0';  /* don't include <CR><LF> */
    			return (int)(buff - tmpbuf);
    		}
    		
    		*buff++ = data;
    		lastdata = data;
    		len--;
    	}
    	
    	return(0);
    }
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>size_t len = sizeof(buff);
    That'll equate to the size of the pointer, not the buffer length. Pass the lenth as a second parameter or something. This is why they were like they were in the other thread
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    ok, if you are passing an array of chars into the function, is there a way to determine the size of the array in the function?

    Code:
    char barf[256];
    ZeroMemory(barf, 256);
    
    PEER Client;
    Client.ReadCRLF(barf); //the function can determine size is 256?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>is there a way to determine the size of the array in the function?
    Not really. The only method is to have a specific character as the last element of the array. Parse the array starting from the beginning until you reach said character, counting as you go. This is a bad method though, for various reasons:

    - the last character could be overwritten in
    error, then the parsing would go all wrong
    - the special character might be placed into the "data" area of the array, which will confuse the parsing routine
    - it's highly inefficient

    Best (and commonly used) method, is to pass the size as a function parameter.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    ok, I changed the function to take len, and it still doesn't work =/

    it doesn't freeze all the way, and I'm pretty sure the server isn't getting anything
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Time to start proper debugging then. Sometimes a packet trace will help you as well. http://www.etherdetect.com/
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    ok, am looking through code, I'm pretty sure what it's doing.

    but

    if (rc < 0 && errno == EINTR) continue;

    why would you continue if rc is less than 0? wouldn't you want to continue if it was greater than 0?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    rc would be -1 if an error had occured. If the error is EINTR, it means the recv() was interupted (maybe by the program receiving a signal for example). In this case, it's a "soft error" and the program should retry. Anything other than EINTR is fatal, and the recv() should not be reattempted.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    where is EINTR defined?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Normally in something like errno.h, but it's none standard. Maybe its best that you simply remove that particular line for now.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM