net FAQ

net FAQ

net FAQ

What does net.listen return?

returns different handles according to type:

typereturn typepublic fieldpublic method
webWebServeraddr、typeclose()
tcpTcpServeraddr、typeclose()
udpUdpSocketaddr、typesend()、close()
wsWsServeraddr、typeclose()

addr is the actual listening address, type is the service type string, close() Used to close a service or socket.

What does net.connect return?

returns different connection handles according to type:

typereturn typefieldcommon methods
tcpTcpClientaddr、typewrite()、send()、read()、close()
udpUdpSocketaddr、typesend()、close()
wsWsSocketaddr、typesend()、write()、close()、on_message()、on_close()、on_error()

TCP and UDP use host, port to configure the connection target; WebSocket uses url to configure the complete connection address.

Callback field names

event callbacks uniformly use snake_case: on_connect, on_message, on_close, on_error.

Port occupation

bind will throw an error when the port is occupied. Development tests can use 127.0.0.1:0 to let the operating system allocate free ports.

What happens when the queue is full

TCP, UDP, and WebSocket background event queues have a fixed upper limit, the default is 4096. When the queue is full, it will not occupy unlimited memory: TCP and WebSocket will close the corresponding connection, and UDP will discard the current message. The number of occurrences can be viewed via BT.stats().net.event_queue_rejected.

Error message

User-visible errors of TCP, UDP, and WebSocket will try to use a unified Chinese format. Common types include port occupied, insufficient permissions, address unavailable, connection refused, connection reset by peer, handshake failure, write queue full, and message size exceeding upper limit.

Stress test script

net-stress-tcp-server.bt / net-stress-tcp-client.bt, net-stress-udp-server.bt / net-stress-udp-client.bt, net-stress-ws-server.bt / net-stress-ws-client.bt are provided under examples/. Start the corresponding server first, and then start the client. When you need to push down resource boundaries, set BT_NET_EVENT_QUEUE, BT_NET_WRITE_QUEUE, or BT_NET_MESSAGE_LIMIT before starting the process.

How to receive binary messages

After setting binary: true in the TCP, UDP, and WebSocket listening configuration, the message parameters of the on_message callback will return Bytes. TCP client synchronous reading can use read_bytes().