aboutsummaryrefslogtreecommitdiff
path: root/examples/Server.hx
blob: 87e00a4944c6b794f794337e0582a041b428dca3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package examples;

import haxe.io.Bytes;
import sys.FileSystem;
import unix.UnixSocket;

class Server {
	static function main() {
	    var server = new UnixSocket("haxe.sock");

	    server.init();
	    server.bind();
	    server.listen(5);

        while (true) {
            trace('Waiting for clients to connect...');
    	    var client = server.accept();
    	    trace('Accepted connection from client with fd ${client.fileDescriptor}');
    	    var receivedData = client.readBytes("Ping".length).toString();
    	    trace('$receivedData');
    	    client.writeBytes(Bytes.ofString("Pong"));
    	    client.close();
	    }

	    if (FileSystem.exists("haxe.sock")) {
	        FileSystem.deleteFile("haxe.sock");
        }
	}
}