std.net
std.net is the first networking slice in cnegative.
It is deliberately small and beginner-first:
- blocking IPv4 TCP and UDP only
- raw socket/listener handles as
int - simple text helpers
- no async runtime
- no HTTP, TLS, or DNS convenience layer
If you are brand new to the language, you can skip this page for now and come back later.
Import
cneg
import std.net as net;Current API
net.is_ipv4(str) -> boolnet.join_host_port(str, int) -> strnet.tcp_connect(str, int) -> result intnet.tcp_listen(str, int) -> result intnet.accept(int) -> result intnet.send(int, str) -> result intnet.recv(int, int) -> result strnet.UdpPacket { host:str; port:int; data:str }net.udp_bind(str, int) -> result intnet.udp_send_to(int, str, int, str) -> result intnet.udp_recv_from(int, int) -> result net.UdpPacketnet.close(int) -> result bool
The easiest functions to understand first
Start with these:
net.is_ipv4(...)net.join_host_port(...)
Example:
cneg
import std.net as net;
import std.strings as strings;
fn:int main() {
let endpoint:str = net.join_host_port("127.0.0.1", 8080);
if strings.eq(endpoint, "127.0.0.1:8080") == false {
free endpoint;
return 1;
}
free endpoint;
return 0;
}What “blocking” means
Blocking means the program waits at that call until the operation finishes.
For example:
accept(...)waits for a clientrecv(...)waits for dataudp_recv_from(...)waits for one datagram
That is simpler for beginners than async/event-loop networking.
TCP in one sentence
TCP is the connected path:
- connect to a peer
- send text
- receive text
- close the socket
UDP in one sentence
UDP is the datagram path:
- bind a socket
- send one packet
- receive one packet
- check who sent it
TCP example
cneg
import std.net as net;
fn:int main() {
let socket:result int = net.tcp_connect("127.0.0.1", 34567);
if socket.ok == false {
return 1;
}
let closed:result bool = net.close(socket.value);
if closed.ok == false {
return 2;
}
return 0;
}UDP example
cneg
import std.net as net;
fn:int main() {
let socket:result int = net.udp_bind("", 34567);
if socket.ok == false {
return 1;
}
let closed:result bool = net.close(socket.value);
if closed.ok == false {
return 2;
}
return 0;
}Ownership notes
net.join_host_port(...)returns an owned stringnet.recv(...)returns an owned string on successnet.udp_recv_from(...)returns a packet wherepacket.hostandpacket.dataare both owned strings
raw handles
Socket and listener handles are plain int values right now. This is explicit and low-level by design.
platform model
Linux and macOS lower through POSIX/BSD sockets. Windows lowers through Winsock.
not full networking
There is still no async/event loop model, HTTP client/server layer, TLS, or DNS convenience API here yet.