Re: Sockets in GTK, how to use?



Excerpts from Jacques Pelletier's message of 2011-01-30 18:37:34 +0200:

I'm using Gnet 2.08 in my application and I would like to convert it to GTK 
for its socket functions.

From the docs, I was able to figure out how to open a connection, but once 
the connection is opened, how do we read and write to a socket?


I'am begginer in this area... But something like this works for me:

GSocket            * socket;
GSocketClient      * client;
GSocketConnection  * connection;
GSocketConnectable * addr;

addr       = g_network_address_new (host, port);
client     = g_socket_client_new ();
connection = g_socket_client_connect (client, addr, NULL, &error);
socket     = g_socket_connection_get_socket (connection);

if (socket) {
        gchar * locator = g_strdup("data to send");
        g_socket_send_with_blocking(socket, locator, strlen(locator), FALSE, NULL, NULL);
        g_free(locator);

        while ( (received = g_socket_receive_with_blocking (socket, buffer, 1024, FALSE, NULL, NULL)) ) {
                for (buffpos = 0; buffpos < received; buffpos++) {
                        g_string_append_c(page, buffer[buffpos]);
                }
                g_debug("\n\n=>> Got %d/%d bytes\n\n", received, total += received);
        }
} else {
        g_debug("%s", error->message);
        g_free(error);
}

PS: something as good as gnet_uri is still missing in glib/gio. I think we should move it from
gnet to glib.


-- 
xmpp:self antono info
http://antono.info/
gopher://antono.info/



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