带套接字的C中的事件驱动模型 [英] Event-driven Model in C with Sockets

查看:234
本文介绍了带套接字的C中的事件驱动模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的对C语言中的事件驱动编程特别是套接字感兴趣,因此我将花一些时间进行研究.

I am really interested in event-driven programming in C especially with sockets so I am going to dedicate some time doing my researches.

让我们假设我想构建一个具有大量文件和网络I/O的程序,就像客户端/服务器应用程序一样,基本上,第一个问题是该模型背后的哲学是什么.在正常的编程中,我会生成新的流程,但是一个流程实际上如何可以满足许多其他请求.例如,有一些Web服务器可以处理连接而无需创建线程或其他进程,而只是一个主要进程.

Let's assume that I want to build a program with much File and Network I/O like a client/server app, basically, the first question is what is the philosophy behind this model. While in normal programming I would spawn new processes, how come a single process can actually serve many other requests. For example, there are some web-servers which can handle connections without creating threads or other processes, just one main process.

我知道这很复杂,但是知道不同的解决方案如何工作总是很高兴.

I know this is complicated but it's always nice to know how different solutions work.

推荐答案

您绝对必须阅读以下内容: http://www. kegel.com/c10k.html .该页面是事件驱动和异步技术的完美概述.

You definitely must read the following: http://www.kegel.com/c10k.html. That page is the perfect overview of event-driven and asynchronous techniques.

但是,快速&肮脏的答案:事件驱动既不是非阻塞也不是异步的.

However, a quick & dirty answer: event-driven is neither non-blocking, nor asynchronous.

事件驱动的意思是,该进程将监视其文件描述符(和套接字),并且仅在某些描述符上发生某些事件(事件是:接收到数据,错误,变得可写...)时起作用.

Event-driven means, that the process will monitor its file descriptors (and sockets), and act only when some event occurs on some descriptor (events are: data received, error, became writeable, ...).

BSD套接字具有"select()"功能.调用时,操作系统将监视描述符,并在描述符之一上发生某些事件后立即返回到进程.

BSD sockets have the "select()" function. When called, the OS will monitor the descriptors, and return to the process as soon as some event on one of the descriptors occurs.

但是,上面的网站有更好的描述(以及有关不同API的详细信息).

However, the website above has much better descriptions (and details about the different APIs).

这篇关于带套接字的C中的事件驱动模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆