Writing a Client/Server Tutorial

Here's a small Client/Server Application that demonstrate the use of the class neko.net.Socket.

The Client :

// file Client.hx
class Client {
    static function main() {
        var s = new sys.net.Socket();
        s.connect(new sys.net.Host("localhost"),5000);
        while( true ) {
            var l = s.input.readLine();
            if( l == "exit" ) {

The Server :

// file Server.hx
class Server {
    static function main() {
        var s = new sys.net.Socket();
        s.bind(new sys.net.Host("localhost"),5000);
        trace("Starting server...");
        while( true ) {
            var c : sys.net.Socket = s.accept();
            trace("Client connected...");
            c.write("your IP is "+c.peer().host.toString()+"\n");

The HXML file to build the two projects :

# client_server.hxml
-neko client.n
-main Client.hx

-neko server.n
-main Server.hx

You can compile using the following command :

haxe client_server.hxml

Then open two terminals, in the first one you can start the server by running :

neko server.n

And in the second one you can start a client by running :

neko client.n

The server is sending to the client some datas that the client is printing, and when "exit" is received, the client exit and the server wait another client.

Multiple Clients

If you want to be able to talk to several clients at the same time you can either the use neko.net.Socket.select method for multiplexing and/or the Thread api to create multiple running threads.

See also the Flash/Neko Chat Example


Keep in mind that Linux target can't bind <1024 port as regular user. Use authbind or run as root.
You will see "Error : Bind failed" if you do so.
version #19841, modified 2013-12-05 16:13:41 by vegetablesalad