-
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;
}
-
-
maybe its the CHAR
int GetFullRecv(CHAR *cMsg)
-
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
-
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.
-
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);
}
-
>>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 ;)
-
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?
-
>>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.
-
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
-
Time to start proper debugging then. Sometimes a packet trace will help you as well. http://www.etherdetect.com/
-
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?
-
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.
-
-
Normally in something like errno.h, but it's none standard. Maybe its best that you simply remove that particular line for now.