4

Does anyone now where i can find some good tutorials / code samples regarding networking in gtk / glib. I'm trying to write a client application that connects to a simple server that echos back what it receives, using Gtk2 and i'm not sure how to go about it. I want to do async read / writes without using threads but i'm not sure what is the best approach. The API reference isn't much help - GIOChannel, GIOStream, GSocketClient, etc - which one to use?

The fallback would be to use blocking IO in another thread.

I'm not interested in portability.

"Rant: How do the GTk / GLib developers expect people to use their framework without good documentation? Why do i even bother when i could do it using QT. I wan't to learn more C, thats why i bother!"

Vlad Balmos
  • 3,372
  • 19
  • 34
  • If you're in it to learn stuff, why not try to use the bare socket API directly? It will teach you quite a bit how networking works behind the scenes, and teach you more about how higher-level APIs works, and will also help your understanding when something goes wrong in those high-level APIs. – Some programmer dude Jan 20 '12 at 08:02
  • i know how to use the low level api. I want to do async IO as not to block the main UI. I'm also not too fond of threads because i don't really know how to use them – Vlad Balmos Jan 20 '12 at 08:06
  • How do you think async sockets works in those APIs? They make them _non-blocking_. It's just one function call to make a socket [non-blocking](http://www.kegel.com/dkftpbench/nonblocking.html), and one other to [`poll`](http://linux.die.net/man/3/poll) if there is data ready to be read or a new connection to be accepted. – Some programmer dude Jan 20 '12 at 08:10
  • 1
    ok, fair enough, but i'm not looking into using the low level IO. i want to use the framework's implementation and not to reinvent the wheel. – Vlad Balmos Jan 20 '12 at 08:16

2 Answers2

5

I use the functions g_socket_client_new(), g_socket_client_connect_async(), g_socket_client_connect_finish().

And then the g_io_stream_get_input_stream() and g_io_stream_get_output_stream() to get the streams and do the real send and receives.

If you really need it, I may have some example code around...

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • that's what i was looking for, just some pointers. Thank you very much! – Vlad Balmos Jan 20 '12 at 12:17
  • one question though, do you use GIOChannel at all, or when should that class be used. Thanks – Vlad Balmos Jan 20 '12 at 12:20
  • I've had the same doubts about GIOChannel vs. all the others. After quite some work, my conclusion is that GIOChannel is to be tought as a GLib wrapper around a FD and select/poll: it calls a callback when the FD is readable/writable, and that's all (great for pipes or character devices, for example). But GIO is a complete wrapper API for handling input/output, more suitable for writing a full network client/server, for instance. – rodrigo Jan 20 '12 at 14:22
0

There is this project named Grsync for windows , it uses Gtk. Browse some code here:- http://sourceforge.net/projects/grsync-win/

Akshay Patil
  • 954
  • 1
  • 12
  • 28