# net 常见问题

## net.listen 返回什么

按 type 返回不同句柄：

| type | 返回类型 | 公共字段 | 公共方法 |
|------|------|------|------|
| web | WebServer | addr、type | close() |
| tcp | TcpServer | addr、type | close() |
| udp | UdpSocket | addr、type | send()、close() |
| ws | WsServer | addr、type | close() |

`addr` 是实际监听地址，`type` 是服务类型字符串，`close()` 用于关闭服务或 socket。

## net.connect 返回什么

按 type 返回不同连接句柄：

| type | 返回类型 | 字段 | 常用方法 |
|------|------|------|------|
| tcp | TcpClient | addr、type | write()、send()、read()、close() |
| udp | UdpSocket | addr、type | send()、close() |
| ws | WsSocket | addr、type | send()、write()、close()、on_message()、on_close()、on_error() |

TCP 和 UDP 使用 `host`、`port` 配置连接目标；WebSocket 使用 `url` 配置完整连接地址。

## 回调字段名称

事件回调统一使用 snake_case：on_connect、on_message、on_close、on_error。

## 端口占用

bind 端口被占用时会抛出错误。开发测试可使用 127.0.0.1:0 让操作系统分配空闲端口。
