Thread: Delimiting messages

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    12

    Delimiting messages

    I've been working on a program that sends periodically sends several values at once to another computer, and I've been working on a way for the receiving computer to parse the received data because messages have been concatenating. I did some reading on a way to do this, and in one book I was reading I found a function that used delimiters for parsing messages, and I modified for my program:

    Code:
    int ReceiveMessage(SOCKET s, char *buf, int maxLen, char *x, char *y, char *z)
    {
    int received=0;
    int delimCount=0;
    int rv;
    char buff[1];
    int count=0;
    char c;
    
    while (delimCount < FIELDSPERMSG)
    {
    rv = recv(s, buff, 1, 0);
    
    
    c=buff[0];
    buf[count]=c;
    count++;
    
    //printf("blah \n");
    if (rv < 0)
    exit(1);
    else if (rv == 0)
    exit(1);
    if (*(buff) == DELIMCHAR) /* count delimiters */
    delimCount += 1;
    
    received += 1;
    if(delimCount==1)
    {
    strcpy ( x, buf);
    printf("Server: x position received - %ld.\n", x);
    buf=" ";
    }
    }
    
    else if(received==2)
    {
    strcpy ( y, buf);
    printf("Server: y position received - %ld.\n", y);
    }
    
    return received;
    }
    The idea of the function is to receive one char at a time, and store the characters in a buffer until it reaches a space, at which point you know that you've read one of the values being sent in a message. The process is repeated and when you've reached the point where you've read three delimiters, you know you've read one complete message, and theoretically, you should be able to move on to the next message.

    For some reason I'm not seeing though, the program seems to be hanging in execution of the function. Any suggestions?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > The idea of the function is to receive one char at a time
    That is S.L.O.W.. Why not just read say, 1KB and parse that? Continuing in that fashion.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    12
    Quote Originally Posted by zacs7 View Post
    > The idea of the function is to receive one char at a time
    That is S.L.O.W.. Why not just read say, 1KB and parse that? Continuing in that fashion.
    That's a good idea. Would I be able to specify a size rather than character length with the recv() function?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Messages Between Different Architectures
    By Cell in forum Linux Programming
    Replies: 5
    Last Post: 05-07-2009, 03:40 AM
  2. Spy++ view messages posted/sent to a control, or from it?
    By hanhao in forum Windows Programming
    Replies: 2
    Last Post: 06-24-2007, 11:07 PM
  3. Sending windows messages
    By Ideswa in forum Windows Programming
    Replies: 2
    Last Post: 03-02-2006, 01:27 PM
  4. Intercepting messages
    By ScriptBlue in forum Windows Programming
    Replies: 2
    Last Post: 06-23-2005, 12:10 AM