【发布时间】:2013-11-30 18:08:09
【问题描述】:
我想读取和处理来自客户端的所有行,但似乎一次只读取一行。我认为在读取数据时有一个循环会读取所有数据,但似乎并非如此。 如果读取不完整,我将在下一步中从该索引处继续读取。
我有这样的事情:
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);
}
}
【问题讨论】:
标签: c string sockets client readline