HaXe Wiki
Advertisement

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 neko.net.Socket();

s.connect(new neko.net.Host("localhost"),5000);

while( true ) {

var l = s.input.readLine();

trace(l);

if( l == "exit" ) {

s.close(); break;

}

}

}

}

The Server :

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

The HXML file to build the two projects :


  1. client_server.hxml

-neko client.n -main Client.hx

--next -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 /api/neko/vm/Thread api to create multiple running threads.

See also the /doc/flash/chat

Advertisement