获取网页与 C++ 进行通信应用

发布于 2025-01-06 18:14:08 字数 443 浏览 0 评论 0原文

我想要实现的是让网页向 C++ 应用程序发送消息,但这需要从 C++ 到 Web 服务器以及从 Web 服务器到客户端的某种形式的连接。

我想到的一种方法是拥有一个存储消息的 MySQL 数据库,并且 C++ 应用程序使用 libCURL 通过 php 脚本向 Web 服务器询问任何新消息。它必须每秒左右轮询一次,这有点浪费。此外,每当您想从网页发送消息时,您都必须将表单提交回 Web 服务器,这会很慢。

我还在研究 HTML5 websockets,认为可能可以在客户端和 C++ 应用程序之间创建 TCP 连接,但我不认为这就是它们的工作方式。 websockets 是否设计为通过服务器在两个 HTML5 应用程序之间路由消息,或者理论上您可以使用 HTML5 应用程序直接与服务器对话(可能运行 php 脚本,或发送普通 TCP 数据包)?

任何有关我如何实现这一目标的建议都将受到欢迎。

谢谢。

What I want to achieve is to have a webpage send a message to a C++ application, but this requires some form of connection from the C++ to the web server, and from the web server to the client.

One way I thought of doing it would be to have a MySQL database which stores a message, and the C++ app uses libCURL to ask the web server via a php script for any new messages. It would have to poll it every second or so, which would be a bit of a waste. In addition, whenever you wanted to send a message from the webpage, you'd have to submit the form back to the web server, which would be slow.

I was also looking at HTML5 websockets, thinking that it might be possible to create a TCP connection between the client and the C++ app, but I don't think that's how they work. Are websockets designed to route messages between TWO HTML5 applications via the server, or could you theoretically use a HTML5 application to talk directly to the server (maybe run a php script, or send a normal TCP packet)?

Any suggestions for how I could achieve this would be welcomed.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绾颜 2025-01-13 18:14:08

如果您想从您的网站进行远程过程调用,我建议您查看 SOAP。 SOAP 与 PHP 以及 gSOAP 您可以轻松地将服务器接口嵌入到您的 C++ 应用程序中,或者在必要时反之亦然。

如果您只有非常简单的消息要传递给您的应用程序,那么您的数据库想法也可以,但是您的应用程序将必须继续检查是否有可用的新消息(这意味着大量不必要的流量,至少如果您的数据库与您的应用程序不在同一台机器上),您必须自己处理这些检查,提出模型等。

另一方面,SOAP 将您的数据直接发送到服务器应用程序,并处理所有讨厌的事情例如类型安全或隐式序列化。

您可以在 万维网联盟。 gSOAP 的 用户指南< 中还有有关如何使用 C++ 设置 SOAP 服务器的各种示例/a>.

If you want to do remote procedure calls from your website, I'd recommend taking a look at SOAP. SOAP works perfectly with PHP, and with gSOAP you can easily embed a server interface into your C++ application, or vice versa if necessary.

If you have only very simple messages to bring across your application, your database idea would work as well, but your application will have to keep on checking whether there is a new message available (which means a lot of unnecessary traffic, at least if your database is not on the same machine as your application), and you will have to deal with these checks yourself, come up with models, etc.

SOAP on the other hand sends your data directly to your server application, and handles all the nasty stuff like type safety or serialization implicitly.

You can find a lot of documentation and examples, neatly explaining how SOAP is defined and how it works from the World Wide Web Consortium. There are also various examples of how to set up your SOAP server in C++ in gSOAP's User Guide.

情绪 2025-01-13 18:14:08

如果您正在编写 C++ 程序,您可以编写它来利用 CGI,以便您的 Web 服务器将您的 C++ 程序作为“脚本”运行来处理相关的 http 请求,并且您将直接从 C++ 输出 http 响应程序。

http://en.wikipedia.org/wiki/Common_Gateway_Interface

If you're writing the C++ program you can just write it to make use of CGI so that your web server will run your C++ program as a 'script' to handle the relevant http requests and you will directly output the http response from your C++ program.

http://en.wikipedia.org/wiki/Common_Gateway_Interface

自在安然 2025-01-13 18:14:08

这并不容易。您应该查看 Google 的解决方案:

称为 Cloud to Device Messaging Framework,用于向 Android 设备发送消息。

你正在做的事情称为推送。 这是来自 Google IO 的相关视频。

只需将您的 C++ 应用程序视为 Android 设备即可。

您的 C++ 应用程序将通过某个 tcp/ip 端口与您创建的仅等待连接的服务建立连接。您的应用程序将告诉服务器运行该应用程序的用户的 ID,或者如果它是多播的,您的应用程序将只等待服务向其发送消息。

您的网络服务器可以使用进程间通信、命名管道、命名互斥体等来指定您的服务,以通过所有打开的 TCP/IP 连接发送消息。

This is not very easy. You should look at Google's solution to this:

Called Cloud to Device Messaging Framework used for sending message to Android devices.

What you are doing is called Push. Here is a video on this from Google IO.

Just think of your c++ application as the Android Device.

Your C++ app would make a connection to the server over some tcp/ip port to a service you create that just waits for connections. Your app would tell the server the ID of the user running the app or if it was multicast your app would just wait for the sevice to give it a message.

Your webserver can use interprocess comunication, namedpipes, named mutexes, etc to single your service to send a message down all the open TCP/IP connections.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文