>>better to lump all possible data together into 1 message
Yes, do as few system calls as possible by sending chunks of data.
>>so how would I implement this?
There are a few ways, revolving around message length indicators and message terminators.
For example:
At the start of each logical message, put a length indicator. Something like:
>005Hello005World
This contains two logical messages. The length of the length field is ALWAYS 3 bytes, so you read that many first, determine the actual message length, then read that many bytes. Of course, you might need to do more than one read to get that many.
OR
Use a terminating character, like \0:
>Hello\0World\0
This is two messages again, each terminated with a \0. Read in a chunck of data (arbiturary size), and parse the buffer for the \0.
Here is some sample source for reading:
Code:
/* readn - read exactly n bytes */
int readn(SOCKET fd, char *bp, size_t len)
{
int cnt;
int rc;
cnt = len;
while (cnt > 0)
{
rc = recv(fd, bp, cnt, 0);
if (rc < 0) /* read error? */
{
if (errno == EINTR) /* interrupted? */
continue; /* restart the read */
return(-1); /* return error */
}
if (rc == 0) /* EOF? */
return(len - cnt); /* return short count */
bp += rc;
cnt -= rc;
}
return(len);
}
Code:
/* readline - read a newline terminated record */
int readline(SOCKET fd, char *bufptr, size_t len)
{
char *bufx = bufptr;
static char *bp;
static int cnt = 0;
static char b[1500];
char c;
while (--len > 0)
{
if (--cnt <= 0)
{
cnt = recv(fd, b, sizeof(b), 0);
if (cnt < 0)
{
if (errno == EINTR)
{
len++; /* the while will decrement */
continue;
}
return(-1);
}
if (cnt == 0) return(0);
bp = b;
}
c = *bp++;
*bufptr++ = c;
if (c == '\n')
{
*bufptr = '\0';
return(bufptr - bufx);
}
}
set_errno(EMSGSIZE);
return(-1);
}
Code:
/* readcrlf - read a CR/LF terminated line */
int readcrlf(SOCKET s, char *buf, size_t len)
{
char *bufx = buf;
int rc;
char c;
char lastc = 0;
while (len > 0)
{
if ((rc = recv(s, &c, 1, 0)) != 1)
{
/*
* If we were interrupted, keep going,
* otherwise, return EOF or the error.
*/
if (rc < 0 && errno == EINTR) continue;
return(rc);
}
if (c == '\n')
{
if (lastc == '\r') buf--;
*buf = '\0'; /* don't include <CR><LF> */
return(buf - bufx);
}
*buf++ = c;
lastc = c;
len--;
}
set_errno(EMSGSIZE);
return(-1);
}
These samples are from this book by Jon Snader