I am reading from a TCP socket that you cannot guess when the end of the line will be reached. For this you need something like this:
std::string line;
char buf[1024];
int n = 0;
while(n = read(fd, buf, 1024))
{
const int pos = std::find(buf, buf + n, '\n')
if(pos != std::string::npos)
{
if (pos < 1024-1 && buf[pos + 1] == '\n')
break;
}
line += buf;
}
line += buf;
Assuming you use "\ n \ n" as a delimiter. (I have not tested this piece of code ;-))
The UDP socket is another story. The emitter can send a bag containing an entire line. The recipient is guaranteed to receive the package as a single unit. If he receives it, because UDP is not as reliable as TCP, of course.
source
share