net — TCP networking

Requires: nothing — uses OS sockets (Winsock2 on Windows, BSD sockets on macOS/Linux)

#include "net"

Provides session-based LAN multiplayer networking with named players and a message queue.


Method Args Out Description
Net:Host playerName, maxPlayers, sessionName Number Start TCP server on port 2310; returns handle. Throws NetException on failure
Net:Join playerName, sessionName, hostIP Number Connect to host; returns handle. Throws NetException on failure
Net:Check handle void Poll for new connections and incoming data (call each loop tick)
Net:Send handle, msg Boolean Broadcast text message to all peers
Net:SendTo handle, target, msg Boolean Send text message to named peer
Net:SendData handle, code, msg Boolean Send typed data message (integer code + string payload)
Net:GetMessage handle sender, msg, Boolean Pop next text message; got=true if a message was available
Net:GetData handle sender, code, msg, Boolean Pop next data message; got=true if available
Net:Quit handle void Notify peers, close socket, release handle

Throws NetException on Host/Join failure. See Object — Exception.

Var:Number conn;
try {
    Net:Host("server", 4, "game", conn);
} catch (NetException e) {
    Konsol:Print("Host failed: ${e.message}");
}
Konsol:Print("Hosting on port 2310, handle: ${conn}");

Var:String sender;
Var:String msg;
Var:Boolean got;
while (true) {
    Net:Check(conn);
    Net:GetMessage(conn, sender, msg, got);
    if (got) { Konsol:Print("${sender}: ${msg}"); }
}

Building from source

No extra dependency — uses ws2_32 on Windows (built-in) and BSD sockets on Linux/macOS.

make -C net_plugin