C++如何接受服务器推送数据?

发布于 2024-12-11 03:23:42 字数 283 浏览 0 评论 0原文

我的情况:我想创建一个业余爱好项目来改进我的 C++ 涉及实时/延迟编程。

我决定编写一个小型 Java 程序,它将向客户端发送大量随机股票价格,客户端将用 C++ 编写并接受所有价格。

我不希望 C++ 客户端必须轮询/有一个 while 循环,即使没有数据,它也会不断检查数据。

我对此有哪些选择?如果拥有 C++ 服务器更容易实现,那么这不是问题。

我想对于初学者来说,我将不得不使用 boost ASIO 包来进行网络?

我将在 Windows 7 上执行此操作。

My situation: I would like to create a hobby project for improving my C++ involving real-time/latency programming.

I have decided I will write a small Java program which will send lots of random stock prices to a client, where the client will be written in C++ and accept all the prices.

I do not want the C++ client to have to poll/have a while loop which continuously checks for data even if there is none.

What options do I have for this? If it's easier to accomplish having a C++ server then that is not a problem.

I presume for starters I will have to use the boost ASIO package for networking?

I will be doing this on windows 7.

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

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

发布评论

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

评论(3

丿*梦醉红颜 2024-12-18 03:23:42

为什么不让 Java 服务器接受连接,然后等待一段时间。例如10秒。在此时间内,如果数据可用,请发送数据并关闭连接。

然后,C++ 客户端可以有一个线程,每当前一个连接完成时,该线程就会打开一个连接。

当没有新数据时,这应该会提供相当低的延迟,而无需经常创建连接。

这基本上是 Comet Web 编程模型,用于许多应用程序。

Why not just have the Java server accept connections and then wait for some duration of time. e.g. 10 seconds. Within that time if data becomes available, send it and close the connection.

Then the C++ client can have a thread which opens a connection whenever the previous one has completed.

That should give quite low latency without creating connections very often when there is no new data.

This is basically the Comet web programming model, which is used for many applications.

银河中√捞星星 2024-12-18 03:23:42

考虑一下 Web 服务器如何接收数据。当访问 URL 时,数据将被推送到服务器。服务器不需要轮询客户端(或者实际上知道客户端的任何信息,除了向客户端推送字节的服务之外)。

您可以使用 Java servlet 通过 HTTP 接受数据并以这种方式编写代码。同样,boost::asio 有一个服务器示例 这应该可以帮助你开始。在幕后,您可以启用持久 HTTP,以便连接不会频繁打开/关闭。这将使编码模型变得更加简单。

Think about how a web server receives data. When a URL is accessed the data is pushed to the server. The server need not poll the client (or indeed know anything about the client other than its a service pushing bytes towards it).

You could use a Java servlet to accept the data over HTTP and write the code in this fashion. Similarly, boost::asio has a server example that should get you started. Under the hood, you could enable persistent HTTP so that the connections aren't opened / closed frequently. This'll make the coding model much simpler.

夏の忆 2024-12-18 03:23:42

我不希望 C++ 客户端必须轮询/有一个 while 循环
不断检查数据

不断检查有人必须检查

。不一定是你。我从未使用过 boost ASIO,但它可能提供回调注册。如果是,那么只需向 boost 注册一个回调函数,boost 就会等待并在获取数据时给您回调。

当然,其他选择是您使用一些同步函数。就像(不是真正的函数)Socket.read() 一样,它会阻塞线程,直到套接字中有数据或关闭为止。但在这种情况下,您将奉献自己的线程。

--编辑--

关于通信本身。只需选择任何 IPC 机制(套接字/管道/文件/...),我认为有人已经描述过一种机制。一旦您发送了数据,数据本身就会被您“编码”和“解码”,因此您可以创建自己的协议。例如 "%%=##" 其中 "%%"、=##(标记标记开始、中间和结束),您在发送方添加并在接收方删除以获取股票名称和价格。

您可以根据您的需求进一步开发协议。就像您还可以发送买入/卖出建议或包含主要证券交易所新闻的短信提醒消息。只要您的客户端和服务器了解数据是如何“编码”的,您就可以了。

最后,如果您想保护通信安全(并且假设您没有使用某些安全层(SSL)),那么您可以加密数据。但这是不同的章节。 :)

HTH

I do not want the C++ client to have to poll/have a while loop which
continuously checks for data

Someone HAS to.

Need not be you. I've never used boost ASIO, but it might provide a callback registration. If yes, then just register a callback function of yours with boost, boost would do the waiting and give you a call back when it gets some data.

Other option is of course that you use some functions which are synchronous. Like (not a real function) Socket.read() which blocks the thread until there is data in the socket or it's closed. But in this case you're dedicating a thread of your own.

--edit--

Abt the communication itself. Just pick any IPC mechanism (sockets/pipes/files/...), someone already described one I think. Once you send the data, the data itself is "encoded" and "decoded" by you, so you can create your own protocol. E.g. "%%<STOCK_NAME>=<STOCK_PRICE>##" where "%%", = and ## (markers to mark start, mid and end) that you add on sender side and remove on receiver side to get stock name and price.

You can develop the protocol further based on your needs. Like you can also send buy/sell recommendation or, text alert msgs with major stock exchange news. As long as your client and server understand how the data is "encoded" you're good.

Finally, if you want to secure teh communication (and say you're not using some secure layer (SSL)) then you can encrypt the data. But that's a different chapter. :)

HTH

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