【发布时间】:2011-12-20 11:02:17
【问题描述】:
情况:我正在用 c 语言创建一个服务器守护进程,它接受大量的同时连接,并且客户端将向服务器发送数据。我目前将每个客户端连接都生成到一个新线程中。
问题:如果客户端非常快速地发送多行内容(例如,在不到一秒的时间内发送 10 行数据),服务器将看到前两行,但看不到其余行.
问题:如何将来自客户端的数据“排队”(c 中的recv 命令)?这是需要select 或poll 的吗?基本上,我想确保任何客户端都可以非常快速地发送大量数据,而不必担心任何内容被丢弃。如何实现?
示例代码:(注意:下面的代码显然已经过大量修改,尤其是通过删除错误检查。我试图修改我的代码,以便使问题/解决方案清晰而不会陷入困境降低无关部分的语义。请不要在这里陷入任何非标准或缺失的元素)
//this function handles the threads
void *ThreadedFunction(void *arg) {
// do some stuff, like: pull vars out of mystruct
int nbytes;
char buf[256];
while(1) {
if((nbytes=recv(conid, buf, sizeof buf, 0)) <= 0) {
//handle break in connection
} else {
//for this example, just print out data from client to make my point
buf[nbytes] = 0;
printf("%s\n",buf);
}
}
}
//main just sets up the connections and creates threads
int main(int argc. char *argv[])
{
// bind(), listen(), etc... blah blah blah
while(1) {
conid = accept(...); //get a connection
// ... build mystruct to pass vars to threaded function ...
pthread_t p;
pthread_create(&p,NULL,ThreadedFunction,&mystruct); //create new thread
}
}
【问题讨论】:
-
recv 是否应该读取 '(sizeof buf)-1' 以便为终止 null 留出空间? mystruct 是 malloced,我猜?您没有定义“行”是什么,但通常在一秒钟内(甚至 100 毫秒)写 10 行文本很慢 - 您的 recv() 循环应该很容易跟上这一点,(除非有数百个同时繁忙的客户端连接)。