gnio

UPDATE: GNIO now depends on danw's gresolver branch of GLib, which can be found at one of:

gnio is a socket and networking library built on top of Glib/GObject/GIO. It provides Vala bindings for ease of use.

gnio can be downloaded via git at http://sciyoshi.com/git/gnio.git/ (also available via gitweb)

To compile gnio, you will need the latest SVN version of Vala.

Known Git Remotes

Sample

using GLib;

namespace Test {
    class TestSync : Object {
        public void run() throws Error {
            Resolver resolver = new Resolver();

            InetAddress address = resolver.resolve("www.google.com", null);

            debug("(sync)  resolved www.google.com to %s", address.to_string());

            SocketConnection client = new SocketConnection(new InetSocketAddress(address, 80));

            client.connect(null);

            debug("(sync)  connected to www.google.com");

            string message = "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n";

            client.output_stream.write(message, message.len(), null);

            debug("(sync)  wrote request");

            size_t length;

            DataInputStream input = new DataInputStream(client.input_stream);

            message = input.read_line(out length, null).strip();

            debug("(sync)  received status line: %s", message);

            client.close();
        }
    }

    class TestAsync : Object {
        MainLoop loop;
        InetSocketAddress socket_address;
        SocketConnection client;

        public void run() {
            loop = new MainLoop(null, false);

            Resolver resolver = new Resolver();

            resolver.resolve_async("www.google.com", null, (sender, result) => {
                InetAddress address;

                try {
                    address = ((Resolver) sender).resolve_finish(result);
                } catch (Error ex) {
                    debug(ex.message);
                    loop.quit();
                    return;
                }

                debug("(async) resolved www.google.com to %s", address.to_string());

                socket_address = new InetSocketAddress(address, 80);

                client = new SocketConnection(socket_address);

                client.connect_async(null, (sender, result) => {
                    try {
                        ((SocketConnection) sender).connect_finish(result);
                    } catch (Error ex) {
                        debug(ex.message);
                        loop.quit();
                        return;
                    }

                    debug("(async) connected to www.google.com");

                    string message = "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n";

                    client.output_stream.write_async(message, message.len(), 1, null, (sender, result) => {
                        try {
                            client.output_stream.write_finish(result);
                        } catch (Error ex) {
                            debug(ex.message);
                            loop.quit();
                            return;
                        }

                        debug("(async) wrote request");

                        size_t length;

                        /* we set the socket back to blocking here for the convenience of DataInputStream */
                        client.socket.set_blocking(true);

                        DataInputStream input = new DataInputStream(client.input_stream);

                        string message = input.read_line(out length, null).strip();

                        debug("(async) received status line: %s", message);

                        client.close();

                        loop.quit();
                    });
                });
            });

            loop.run();
        }
    }

    class Test : Object {
        public static void main(string[] args) {
            try {
                new TestSync().run();
                new TestAsync().run();
            } catch (Error ex) {
                debug(ex.message);
            }
        }
    }
}

Compile this with:

valac --pkg=gio-2.0 --pkg=gnio sock.vala