与 C++ 中的守护进程交互带插座
我正在编写一个守护进程,它需要在后台运行并处理任务,还需要直接从前端接收输入。我一直在尝试使用套接字来处理此任务,但是,由于套接字在等待连接时暂停程序,因此我无法使其正常工作。有办法解决这个问题吗?
我正在使用 http://linuxgazette.net/issue74/tougher.html
感谢您提供的所有帮助
I'm writing a daemon that needs to both run in the background and take care of tasks and also receive input directly from a frontend. I've been attempting to use sockets to take care of this task, however, I can't get it to work properly since sockets pause the program while waiting for a connection. Is there anyway to get around this?
I'm using the socket wrappers provided at http://linuxgazette.net/issue74/tougher.html
Thank you for any and all help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您将需要使用线程来使套接字操作异步。或者使用一些已经实现它的库,最重要的库之一是 增强Asio。
You will need to use threads to make the socket operations asynchronous. Or use some library that has already implemented it, one of the top ones is Boost Asio.
有几种方法可以处理这个问题。最常见的是使用事件循环和类似 libevent 的东西。然后使用非阻塞套接字。
以事件驱动的方式执行此操作可能需要对程序逻辑进行重大转变。但使用线程来实现它有其自身的复杂性,并且显然不是更好的选择。
There are a few ways to handle this problem. This most common is using an event loop and something like libevent. Then you use non-blocking sockets.
Doing this in an event driven fashion can require a big shift in your program logic. But doing it with threads has its own complexities and isn't clearly a better choice.
通常守护进程使用事件循环来避免等待事件的问题。
这是解决您提出的问题的最明智的解决方案(不要等待异步事件)。尽管如此
,通常整个守护进程是在事件循环及其回调架构上构建的,并且可能导致部分重写,因此通常快速而肮脏的解决方案是创建一个单独的线程来处理这些事件,这通常会产生比解决的错误更多的错误。因此,使用事件循环:
Usually the daemons use event loops to avoid the problem of waiting for events.
It's the smartest solution to the problem that you present (do not wait to an asynchronous event). ç
Althought, usually the entire daemon is build over the event loop and it's callback architecture, and can cause a partial rewritting, so usually the quick and dirty solution is creating a separate thread to handle those events wich usually creates more bugs than it solves. So, use an event loop:
根据您的描述,您已经将应用程序分为前端(接收输入)和后端(套接字处理和任务)。如果来自前端的输入是通过套接字发送的(通过后端)而不是从套接字接收输入,那么您似乎正在描述客户端而不是服务器。客户端程序通常不作为守护进程实现。
您已经创建了一个阻塞套接字,并且需要在单独的线程执行中监视线程甚至单独的进程),或者创建一个非阻塞套接字并频繁轮询以获取更新。
LinuxGazette 的链接是网络编程的基本介绍。如果您想更深入一点,请查看 Beej 的网络指南编程中对您可用的各种 API 调用进行了一些详细的解释..并且也许会让您欣赏更多的包装器库,例如 Boost::ASIO。
From your description, you have already divided your application into a frontend (receiving input) and backend (socket handling and tasks). If the input from the frontend is sent over the socket (via the backend) rather receiving input from the socket then it seems like you are describing a client and not a server. Client programs are typically not implemented as daemons.
You have created a blocking socket and need to either monitor in a separate thread execution a thread or even separate process) or make a non-blocking socket and poll frequently for updates.
The link to the LinuxGazette is a basic intro to network programming. If you would like a little more depth then take a look at Beej's Guide to Network Programming where the various API calls available to you are explained in a little detail.. and will, perhaps, make you appreciate more wrapper libraries such as Boost::ASIO.
值得您自己保留对事件循环的控制——它并不复杂,并且提供了灵活性。
事件循环的“C++ 伪代码”。
Can be worth retaining control of the event loop yourself - its no complicated and provides flexibility down the track.
"C++ pseudo-code" for an event loop.