2

this is a general question about GUIs. I never wrote a program with GUI, and now I have to do it for the University. I found that the best solution programming in C is GTK+.

I see many examples, but I still can't understand one thing. My program is a server/client app, so it has to run in background also if there's a small GUI. How could it be realised? I see that to "start" the guy I have to use gtk_main() loop, and the application blocks inside this loop for all the time. Should I use fork() to do other tasks at the same time?

Thanks, sorry if it's a trivial question.


It's difficult to explain it in italian, in english is even worst :)

The program has to listen continously the network (LAN), to see if other users arrives and catch their UDP messages, and at the same time it has to communicate to a specific user if some conditions are verified (for example, if a file in a local folder changes, it has to send this file to the designated user. This could happen with multiple users at the same time). I have only one application that has to be server (sending files) and client (listening to the network) at the same time.

The idea of having a separate "client" which implements the GUI could be interesting.

I never developed a GUI, so I thought to write all my program, and then add the GUI at the end. It's possible?

4
  • 1
    What exactly do you mean by: "My program is a server/client app"? Is it a client that connects to a server (which is a different program)? Or two instances of the same program run, where one is a client and one is a server? Commented Apr 25, 2012 at 15:44
  • Next: "Should I use fork() to do other tasks at the same time?" What other tasks do you want to do, apart from running the GUI? Commented Apr 25, 2012 at 15:44
  • Next: "so it has to run in background also if there's a small GUI": What do you mean by this? I am unclear. Commented Apr 25, 2012 at 15:45
  • The program should be both server and client, at the beginning it's a server, scanning the network, if an aswer is received, I fork the process and the child start the client part of the app, and the father remains listening the channel. The other tasks are transmitting messages throw tcp sockets, and sending files. Commented Apr 25, 2012 at 16:59

2 Answers 2

2

You would run other tasks by asking the main loop to run them for you. This is handled automatically if you do IO with GIO (see http://developer.gnome.org/gio/stable/). But in the general case you would use functions such as g_idle_add(), g_timeout_add(), etc. as described here: http://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html and perhaps g_io_add_watch() here: http://developer.gnome.org/glib/stable/glib-IO-Channels.html

If you need to do blocking IO (something like read()/fread() calls) then you have to spawn a thread and have that thread communicate back to the GUI loop by adding an idle handler. This is tricky to understand if you're new to main loops or threads, so using GIO is a superior option since it does this for you.

You can also use fork() to spawn a process to do your IO, but then you need to use IO to communicate with your process, so you can't really avoid having to do IO in the UI process.

Sign up to request clarification or add additional context in comments.

2 Comments

With this method may I use the function I already wrote or I have to rewrite them?
It really depends on what you wrote ;-)
0

Can you provide some details about what specifically your program is doing? Your current description is vague enough that it's hard to really understand what you are asking.

Here's some generic advice based on my interpretation of the original question.

You said that your program "is a server/client app, so it has to run in background also if there's a small GUI". Note that servers typically don't have integrated GUIs; they're usually designed to run silently and invisibly in the background. It's not uncommon to have a graphical interface for configuring or checking the status of the server, but it's traditionally implemented as a separate, standalone application (a client of sorts) that merely connects to the server to retrieve status information and send configuration commands. You won't have the GUI running the entire time that the server is running, so you don't want the server to be burdened with the additional overhead.

As far as your client goes, GUIs are typically implemented using callbacks. When a UI widget is created, it is given a pointer to a function that will be called whenever the widget is clicked, modified, or otherwise acted upon. Sometimes that callback function will simply update another UI widget and return, and sometimes it may require a new thread to be spawned for the purpose of doing something more complex. Again, this will largely depend on exactly what your GUI and application are trying to do.

1 Comment

There are also some tutorials on the GTK website that may be helpful to you: developer.gnome.org/gnome-devel-demos/unstable

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.