[英]How to put socket file descriptor in a buffer and continue accepting incoming connections?
I am trying to design a multithreaded web server in C using Pthreads and i am having a problem in accepting more incoming connections without serving them. 我正在尝试使用Pthreads在C中设计一个多线程Web服务器,并且在不提供服务的情况下接受更多传入连接时遇到问题。
I want to put the file descriptor of each recieved connection in a buffer to be pocessed by a thread, Im using default accept(2) for accepting clients connections. 我想将每个接收到的连接的文件描述符放在缓冲区中,以便线程处理,我使用默认的accept(2)接受客户端连接。
should i be using select ? 我应该使用select吗? any suggestion ?
有什么建议吗?
I read this article these days (i didn't try, but looks good): http://www.linuxjournal.com/content/network-programming-enet or libevent too. 这些天,我读了这篇文章(我没有尝试,但是看起来不错): http : //www.linuxjournal.com/content/network-programming-enet或libevent。
If you want code by your own hands, look at (performance ideas): http://www.kegel.com/c10k.html 如果您想自己动手编写代码,请查看(性能方面的想法): http : //www.kegel.com/c10k.html
A common way of doing multi-threaded servers is to create a new thread right after you accept a new connection, and pass the new socket to that thread. 做多线程服务器的一种常用方法是在接受新连接后立即创建一个新线程,并将新套接字传递给该线程。 Something like this:
像这样:
int main(int argc, char *argv[])
{
/* ... */
int client_socket = accept(server_socket);
pthread_create(&thread, NULL, my_connection_handler, (void *) client_socket);
/* ... */
}
void *my_connection_handler(void *argp)
{
int socket = (int) argp;
write(socket, "Hello!\r\n", 8);
close(socket);
return NULL;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.