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?