在 C 中进行连接管理有哪些好方法?
在 C 语言中,当进行网络客户端/服务器设置时,我通常必须进行一些标准 BSD 套接字设置。然后在服务器端,我必须管理多个线程,通常是一个主线程,一个io线程。每个连接都由连接管理器管理,以便您可以在新请求传入时处理连接。
在 C 中进行连接管理有哪些好方法?是否有众所周知的库来处理所有这些?我了解 Boost for C++,但我对 C 和 Python 感兴趣。
谢谢, 陈兹
P.S.很抱歉这个问题没有经过深思熟虑。我会尽快尝试并完善它。
In C, when making a networking client / server setup, I usually have to do some standard BSD socket setup. Then on the server side, I'll have to manage multiple threads, usually a main thread, an a io thread. Each connection is managed by a connection manager so that you can have connections being processed while new requests are coming in.
What are some good ways to do connection management in C? Are there well know libraries to handle all of this? I know about Boost for C++, but I'm interested in C and Python.
Thanks,
Chenz
P.S. Sorry about the not so thought out question. I'll try and polish it up soon.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就我个人而言,我不太喜欢同步 IO 的每连接一个线程模型。我更喜欢 X 线程和 Y 连接池以及异步 IO。您可以根据需要生成线程,或者在连接进入预分配池时对它们进行循环。
如果您想真正棘手,请生成具有生命周期管理的线程,其中新连接进入最新生成的线程,以便可以杀死旧线程。这样,如果一个线程持有一个资源,当它被清理时,该资源将被释放。
您可能想看看 select、poll、epoll、完成池和 AIO。
其中大部分都包含在 libevent 中。
Personally, I am not a huge fan of the one-thread-per-connection model with synchronous IO. I prefer X threads with a pool of Y connections with asynchronous IO. You can spawn threads as needed, or round robin the connections as they come in to a pre-allocated pool.
If you want to be really tricky, spawn threads with lifetime management, where new connections go to the newest spawned thread so the old thread can be killed off. That way if a thread holds on to a resource, when it is cleaned up the resource will be released.
You may want to look at select, poll, epoll, completion pools and AIO.
Most of these are wrapped up in libevent.