如何制作一个从 TCP 套接字接收数据的 GTK 应用程序

发布于 2025-01-14 16:54:02 字数 169 浏览 4 评论 0原文

我正在构建一个 GUI(使用 C),它从另一个通过 TCP 套接字发送数据的应用程序接收要显示的数据。我如何使用 GTK 来做到这一点(只是我应该采取的方法的一般概述)?我做了很多搜索,遇到了有关多线程、GIOchannel 等的内容。现在我比以往任何时候都更加困惑。似乎没有任何关于如何实际实现这一目标的结论性文章或指南。

I am building a GUI (using C) which receives data to display from another application sending the data over a TCP socket. How do I do this using GTK (just a general overview of the approach I should take)? I have done a lot of searching and came across stuff about multithreading, GIOchannel etc. now I'm more confused than ever. There doesn't seem to be any conclusive articles or guides about how to actually achieve this.

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

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

发布评论

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

评论(1

柠檬色的秋千 2025-01-21 16:54:02

基本上有一个重要的规则:

您必须从主线程调用所有 gtk_* 函数。

如果您从另一个线程更新任何小部件,则可能会得到不一致的结果。

当然,您不想在该线程中等待 TCP 数据。

因此我建议您创建一个单独的线程来进行通信。在此线程中,您可以等待数据,如果您收到任何影响 GUI 中显示内容的内容,您可以告诉主线程执行所需的工作。

执行此操作的一个简单方法是使用 g_idle_add() 将回调函数排入队列。然后,该回调函数在主线程上下文中执行,并且可以更新您的小部件。
需要更新的信息可以存储在一些新分配的内存中,该内存传递给此回调,您必须在之后释放它。

There is basically one important rule:

You must call all gtk_* functions from the main thread.

If you update any widgets from another thread, you might get inconsistent results.

Of course, you don't want to wait for TCP data in that thread.

Therefore I would suggest you create a separate thread for doing the communication. In this thread you can wait for data and if you got anything that should affect what you show in your GUI, you can tell the main thread to do the required work.

A simple way to do this is to use g_idle_add() to enqueue a callback function. That callback function is then executed in context of main thread and can update your widgets.
The information what needs to be updated can be stored in some newly allocated memory that is passed to this callback where you have to free it afterwards.

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