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

发布于 2025-01-18 02:29:36 字数 162 浏览 4 评论 0原文

我正在构建一个GUI(使用C),该GUI接收数据以从另一个应用程序显示,该应用程序通过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-25 02:29:36

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

您必须从主线程调用所有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 和您的相关数据。
原文