aboutsummaryrefslogblamecommitdiff
path: root/examples/Server.hx
blob: a0a65c6b24c76b3218f8f4b42388a2d43b559404 (plain) (tree)
1
2
3
4
5
6
7
8
9






                       

                                                 
 


                         


                                                       






                                                                                      
 

                                               
         
     
 
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");
        }
    }
}