Re: [Vala] simple echo client server example



On Tue, 2009-09-01 at 09:55 -0400, Reid Thompson wrote:
Could someone point me to, or post, a simple tcp echo client server
example using the current vala bindings?

thanks,
reid

Could someone help me understand what I'm missing in trying to get a
simple socket server running?

Thanks,
reid

using GLib;

class ServerDemo {
    public static GLib.MainLoop loop;


    bool handler(SocketConnection connection, GLib.Object source) {
        var dis = new DataInputStream (connection.input_stream);
        var dos = new DataOutputStream(connection.output_stream);

        connection.socket.set_blocking(true);
        string message = dis.read_line(null,null).strip();
        stdout.printf("message[%s]\n", message);

        try {
            dos.put_string("back at you:  " + message, null);
        } catch (Error e) {
            stderr.printf("%s\n", e.message);
        }


        return(true);
    }

    public void run () throws Error {
        SocketAddress serversock = null;
        SocketService server = new SocketService();
        InetAddress inetaddr = new InetAddress.loopback(GLib.SocketFamily.IPV4);
        SocketAddress sockaddr = new InetSocketAddress(inetaddr, 7500);
        server.add_address(sockaddr, GLib.SocketType.STREAM, GLib.SocketProtocol.DEFAULT, server, out 
serversock);
        server.incoming.connect(handler);
        server.start();

    }

    public static int main (string[] args) {

        loop = new GLib.MainLoop(null, false);
        try {
            ServerDemo server = new ServerDemo();
            server.run();
        } catch (Error e) {
            stderr.printf ("%s\n", e.message);
            return 1;
        }

        loop.run();

        return 0;
    }
}
-------------------------------
I managed to get a simple threaded example working, but would appreciate
any input as to things that may be incorrect, or that could be
implemented better....

using GLib;
// using Timeout;

bool
handler(SocketConnection connection, GLib.Object source_object) {
    var dis = new DataInputStream (connection.input_stream);
    var dos = new DataOutputStream(connection.output_stream);

    connection.socket.set_blocking(true);
    string message = dis.read_line(null,null).strip();
    stdout.printf("message[%s]\n", message);

    try {
        dos.put_string("back at you:  " + message, null);
    } catch (Error e) {
        stderr.printf("%s\n", e.message);
    }

    return(true);
}

class ServerDemo {
    public static GLib.MainLoop loop;

    public void run () throws Error {
        ThreadedSocketService  server = new ThreadedSocketService(3);
        server.add_inet_port(7500, server);
        server.run.connect(handler);
    }

    public static int main (string[] args) {

        loop = new GLib.MainLoop(null, false);
        try {
            new ServerDemo().run ();
        } catch (Error e) {
            stderr.printf ("%s\n", e.message);
            return 1;
        }

        loop.run();

        return 0;
    }
}




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