Re: Glib::IOChannel in sockets... (2)



First you must create a socket as usual

mSock = socket(AF_INET, SOCK_STREAM, 0);

and connect it using connect();
Then you can create an IOChannel from it:

Glib::RefPtr<Glib::IOChannel> channel;
#ifdef G_OS_WIN32
channel = Glib::IOChannel::create_from_win32_socket(mSock);
#else
channel = Glib::IOChannel::create_from_fd(mSock);
#endif

IOChannel can be created even from Windows (winsock) sockets. After
that you can work with this channel instead of working direct with
socket descriptor mSock. Don't forget to set correct encoding before
reading or writing to newly created channel.
Listening can be done like this:

Glib::signal_io().connect(sigc::mem_fun(*this,
&MyClass::listenCallback), channel, Glib::IO_IN | Glib::IO_HUP,
G_PRIORITY_LOW);

Your MyClass::listenCallback method must handle channel events.
Also, reading documentation is very best practice :)
Pay attention to these pages:
- http://library.gnome.org/devel/glibmm/stable/classGlib_1_1IOChannel.html
- http://library.gnome.org/devel/glibmm/stable/group__MainLoop.html
(see IO functions)
Hope this will help you! Write if you have any questions.

2009/12/22, Glus Xof <gtglus gmail com>:
> Hello guys,
>
> El 21 / desembre / 2009 23:28, Chris Vine
> <chris cvine freeserve co uk> ha escrit:
>> On Mon, 21 Dec 2009 16:36:57 -0500
>> José Alburquerque <jaalburquerque cox net> wrote:
>>
>>> On Mon, 2009-12-21 at 18:42 +0100, Glus Xof wrote:
>>> > Hello guys,
>>> >
>>> > Some days ago, I wrote,
>>> >
>>> > 2009/12/17 Glus Xof <gtglus gmail com>:
>>> > > Hello guys,
>>> > >
>>> > > I was unsuccessfully googling, searching for an explanations
>>> > > about how to use Glib::IOChannel in case of Sockets: a little
>>> > > tutorial, or a code example...
>>> >
>>> > and I truly need to communicate two proceses running in different
>>> > machines (by sockets), both sending & receiving Glib::ustring
>>> > values. For the moment, using socket(), bind(), listen(), accept(),
>>> > connect()... is enough to interchange std::string values, but I get
>>> > "Glib::ConvertError"s with Glib::ustring.
>>>
>>> Maybe setting the encoding would make a difference.
>>>
>>> The following excerpt from the Glib::IOChannel docs[1] seems relevant:
>>>
>>> "Note that IOChannels implement an automatic implicit character set
>>> conversion to the data stream, and usually will not pass by default
>>> binary data unchanged. To set the encoding of the channel, use e.g.
>>> set_encoding("ISO-8859-15"). To set the channel to no encoding, use
>>> set_encoding() without any arguments."
>>
>> Note also that if you are sending UTF-8 over the socket you have no
>> right to assume that a read on the socket will always produce a whole
>> character (UTF-8 characters can be between 1 and 5 bytes long).  If you
>> are not doing a read delimited by, say, line ends, you will need to
>> reassemble your UTF-8.
>>
>> If you want some ideas here is a class that will do that for you:
>> http://cxx-gtk-utils.cvs.sourceforge.net/viewvc/*checkout*/cxx-gtk-utils/c%2B%2B-gtk-utils/c%2B%2B-gtk-utils/reassembler.h
>> http://cxx-gtk-utils.cvs.sourceforge.net/viewvc/*checkout*/cxx-gtk-utils/c%2B%2B-gtk-utils/c%2B%2B-gtk-utils/reassembler.cpp
>
> As for that Mr Albuquerque said, I think that should be possible to
> communicate two processes by sockets using Glibmm & Giomm libraries...
>
> "IOChannel aims to provide portable I/O support for files, pipes and
> sockets, and to integrate them with the GLib main event loop...."
> (http://library.gnome.org/devel/glibmm/stable/classGlib_1_1IOChannel.html)
>
> The problem is that I don't reach to understand how to use them in that
> sense...
>
> How can I "construct" this channel "a la Glibmm & Giomm" ?...
>
> Which method is the equivalent of socket() (if any) ?
> Which method is the equivalent of bind() (if any) ?
> Which method is the equivalent of listen ().... and accept()... and
> connect()...
>
> Is it possible to create a connection based socket connections
> suitable to interchange (in both directions) Glib::ustring values,
> following the client-server architecture ?
>
> Thanks to all,
>
> Glus
> _______________________________________________
> gtkmm-list mailing list
> gtkmm-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtkmm-list
>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]