Gtk_main () and unix sockets

I am working on a chat application using low level C and unix sockets. I managed to create a console version, but I want to create a graphical interface for the application. I would like to use GTK for the graphical interface. my problem is how to "synchronize" the socket and GUI. because I have to call gtk_main () as the last GTK statement, and the application itself is an infinite loop. How to update the GUI when a message arrives?

+4
source share
1 answer

You are faced with the problem that you have several event systems at a time, but only one thread. Gtk + comes with its own event handler, which ultimately boils down to select() , which wakes up on any user input or other gtk event. You yourself want to work with the network using your own event processing, which usually consists of select() on your socket or using sockets in blocking mode.

One solution is to integrate your events into the Gtk + event loop.

You can make Gtk + watch / select() your sockets and call a certain function when their state changes (reading data). See the โ€œCreating New Source Typesโ€ section at http://developer.gnome.org/glib/2.30/glib-The-Main-Event-Loop.html

Another solution would be to use the functionality of Gtk +.

As a rule, you donโ€™t want to do something special with sockets that are not easy to port using Glib IO Channels. See http://developer.gnome.org/glib/2.30/glib-IO-Channels.html

The third solution is to start a second thread that processes your network, for example. with posix threads or gtk + threading functionality.

Separating the GUI from the working part of your application is, in general, a good idea. However, for the chat application, it probably does not provide any benefit compared to other solutions. See http://developer.gnome.org/glib/2.30/glib-Threads.html

+9
source

Source: https://habr.com/ru/post/1390523/


All Articles