Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

[C/C++] Backlog issue

Name: Anonymous 2007-08-14 12:14 ID:0gEP3LHW

I'm trying to come up with a good way to manage this buffer. However, with my current method, they are sometimes cut short, and there is a large backlog. For example, say the buffer contains "one\ntwo\nthree\nfour\n". It will process "one\n" and stop there. Then, when I recv five\n, it will finally process two\n. So on and so forth, it creates a huge backlog that processes older packets (finally) whenever it receives a new one. I'm fairly new to socket programming, and my buffer handling isn't quite up to par, I suppose. For anyone wondering, the reason it appends the buffer is because the end of the previous buffer may have a cut off command (for instance "sev", and the next receive will have "en\n"). If any of you could help me here, it'd be greatly appreciated. Function below.

bool sockRead()
{
    memset(tmpbuffer, '\0', strlen(tmpbuffer));
    length = recv(ircSocket, buffer, 2048,    0);
    if(length != SOCKET_ERROR)
    {

        char *scout = strncat(nbuffer, buffer, sizeof(nbuffer) - strlen(nbuffer) - 1);
        FILE *file;
        file = fopen("output.txt","a+");
        fprintf(file, "\nSCOUT: %s\n", scout);
        fclose(file);
        if(char *end = strchr(nbuffer, '\n') + 1)
        {
            strncpy(tmpbuffer, nbuffer, end - nbuffer);
            int x = strlen(nbuffer);
            memmove(nbuffer, end, x);
            nbuffer[x] = '\0';
            if(msgHandler() != true)
            {
                return false;
            }
        }
        return true;
    }
    printf("Line is dead.\n");
    return false;
}

Name: 1 2007-08-14 12:15 ID:0gEP3LHW

I forgot to mention... if I do something like change the 'if' to a 'while', a strange exception is thrown...marking "00000000()" and providing no source-code breakpoint.

Name: Anonymous 2007-08-14 12:42 ID:Heaven

>>2
excuse me is tmpbuffer and buffer global variables?
post all your fucking source.


if(char *end = strchr(nbuffer, '\n') + 1)

FUCKING BUGGY. end will always be true except if strchr returns UINT_MAX.
correct:

char *end;
if(end = strchr(nbuffer, '\n')) {
end++;
/* rest */
} else {
/* not found */
}

Name: Anonymous 2007-08-14 12:55 ID:CQM6hLeV

First of, let me say that i have absolutly NO experience with socket programming in C/C++

in your example, in your second if-statement, you are not making any logical comparison, you are setting (char*) end to the return value of strchr(nbuffer,'\n') plus 1.
So depending on what compiler you use, it will always be true, or always be false.

Im guessing that what you are trying to do is to determine if your buffer contains '\n' and if so set 'end' to the next adress in your buffer and then execute whats in your scope after the if.

if that is what you really want to do you could try this:
       
char *end=strchr(nbuffer,'\n')+1;
if(end!=NULL)//old line was: if(char *end = strchr(nbuffer, '\n') + 1)
{
   strncpy(tmpbuffer, nbuffer, end - nbuffer);
   //..more code inside
}

Name: Anonymous 2007-08-14 13:10 ID:Heaven

>>3 here
>>4

char *end=strchr(nbuffer,'\n')+1;
if(end!=NULL)//old line was: if(char *end = strchr(nbuffer, '\n') + 1)

god, another idiot.
OYOU'RE DOING THE SAME FUCKING THING YOU FUCKING IDIOT
TAKE A FUCKING LOOK AT MY FUCKING CODE
GODFUCKING FAMN

Name: Anonymous 2007-08-14 14:45 ID:CQM6hLeV

>>5
>>4 here
I stand corrected, missed that by adding one it's never a null pointer.
Also, you had not answerd when i wrote my reply.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List