Example for a simple Socket Server?



Hi,

I am currently experimenting with GJS
and try to do a simple socket server just for learning purposes.


This is what I have so far:


    const GLib = imports.gi.GLib
    const Gio = imports.gi.Gio;

    let s = new Gio.Socket({
            family: Gio.SocketFamily.IPV4,
            protocol: Gio.SocketProtocol.UDP,
            type: Gio.SocketType.DATAGRAM
    });

    let address = new Gio.InetSocketAddress({
            address: Gio.InetAddress.new_any(Gio.SocketFamily.IPV4),
            port: 4444,
    });

    s.init(null);

    s.bind(address, true);

    while(true) {

        let buf = GLib.ByteArray.new(256);

        for (let ii=0; (ii<256); ++ii) {
            buf[ii]= 0;
        }

        buf[256]= 0;

        let n = s.receive(buf, null );

        print(n);
        print(buf);
    }



I can connect to it via

  nc -u 127.0.0.1 4444


If I type in an "a" for example,
the socket server prints:

  2
  <blank>

So, it doesn't echo the input from netcat
and only prints the size of the input string.


Any idea what I'm doing wrong?

Thanks for your help...

Cheers,

Sven

Attachment: signature.asc
Description: OpenPGP digital signature



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