TcpListener
public struct TcpListener { /* private fields */ }A bound, listening TCP server socket.
Created by TcpListener.bind(port:) — sets SO_REUSEADDR,
binds to INADDR_ANY:port, and calls listen(2) with backlog
128. Accept connections via accept(), which blocks until
the next client arrives. The owned fd is closed by the deinit.
Examples
let listener = match TcpListener.bind(8080) {
.Ok(l) => l,
.Err(e) => return .Err(e)
};
while true {
match listener.accept() {
.Ok(stream) => /* handle stream */ {},
.Err(e) => break
}
}
Representation
A single Int32 field — the listening socket fd.
Memory Model
Owns its fd; closed on drop.
Properties
var fd: Int32
var fd: Int32Initializers
init(Int32)
init(Int32)Internal — wraps an existing fd. Callers should use bind(port:).
Methods
public func accept() -> Result[TcpStream, IoError]
public func accept() -> Result[TcpStream, IoError]Blocks until the next client connects, then returns it as a TcpStream.
Discards the client's address — pass non-null pointers to
libc.accept directly if you need it. Each accepted
connection has its own fd, independent of the listener.
Errors
Returns Err(IoError.last()) if accept(2) fails — common
causes include EINTR (interrupted by signal) and
EMFILE (per-process fd limit).
public static func bind(UInt16) -> Result[TcpListener, IoError]
public static func bind(UInt16) -> Result[TcpListener, IoError]Creates a server socket bound to 0.0.0.0:port with SO_REUSEADDR and a backlog of 128.
Walks the full setup — socket → setsockopt → bind →
listen — and cleans up the partial fd on any failure.
Errors
Returns Err(IoError.last()) (captured errno) at any of the
four steps; the most common case is EADDRINUSE if another
process holds the port and SO_REUSEADDR is not enough.
Examples
let listener = TcpListener.bind(8080);
public func rawFd() -> Int32
public func rawFd() -> Int32Returns the underlying listening fd without giving up ownership.
Defined in lang/std/net/socket.ks