Reputation: 109
I want to read and process all lines from the clients but it seems like only one line is being read at a time. I thought having a loop while there is read data would read it all but it doesn't seem to be the case. If the read is incomplete, I will continue it off at that index in the next step.
I have something like this:
if (select(maxfd + 1, &fdlist, NULL, NULL, NULL) < 0) {
perror("select");
} else {
if (FD_ISSET(listenfd, &fdlist)) {
newclientconnection();
}
// see which clients have activity
for (p = head; p; p = p->next) {
if (FD_ISSET(p->fd, &fdlist)) {
// want to read all lines from client
while ((n = read(p->fd, p->buf + lastindex, MAX-p->lastindex) > 0) {
p->lastindex += n;
}
if (n==0) {
removeclient(p);
}
// want to process all the lines
process(p->buf);
}
}
Upvotes: 0
Views: 104
Reputation: 1487
You have blocking in this line:
while ((n = read(p->fd, p->buf + lastindex, MAX-p->lastindex) > 0))
On the last iteration, the while
cond. waits for input from read
. but the input already read. so, waiting for new input.
If you assume that you gona get data bigger than buffer (therefore you put read
into while
condition), you need to define timeout (select
etc) for read
OR define special symbol ("\r\n\r\n" for example) to determine "this is end of the data". Otherwise, the while
loop will wait forever for more data.
Upvotes: 1