Requires: nothing - uses OS sockets (Winsock2 on Windows, BSD sockets on macOS/Linux)
#include "kse_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:OnMessage |
handle, fn |
void | Register callback fired by Net:Check for each text message |
Net:OnData |
handle, fn |
void | Register callback fired by Net:Check for each data message |
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}");
// Callback style - function fires for each message during Net:Check
function onMessage(String sender, String msg) {
Konsol:Print("${sender}: ${msg}")
}
Net:OnMessage(conn, onMessage)
while (true) {
Net:Check(conn)
}
// Poll style - manual dequeue
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}"); }
}
// Net host test - run this first, then test_net_client.ks in another terminal
//
// After installing:
// minks install kse_net (run once from inside net_plugin/)
// minks test_net_host.ks
//
// Without installing:
// minks --plugin-path . test_net_host.ks
#include "kse_net"
Var:Number conn;
try {
Net:Host("host", 4, "test", conn)
} catch (NetException e) {
Konsol:Print("Host failed: ${e.message}")
return
}
Konsol:Print("Hosting on port 2310, handle: ${conn}")
Var:String sender, msg;
Var:Boolean got;
while (true) {
Net:Check(conn)
Net:GetMessage(conn, sender, msg, got)
if (got) {
Konsol:Print("${sender}: ${msg}")
}
Konsol:Delay(10)
}
No extra dependency - uses ws2_32 on Windows (built-in) and BSD sockets on Linux/macOS.
make -C net_plugin