|
NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
|
UDP Socket类 更多...
#include <udp_socket.hpp>
Public 成员函数 | |
| udp_socket ()=default | |
| 默认构造函数 | |
| void | open (int family=AF_INET) |
| 打开UDP socket | |
| ssize_t | send_to (memory_view< const char > data, const ip_address &endpoint, int flags=0) |
| 向指定端点发送数据报 | |
| ssize_t | send (memory_view< const char > data, int flags=0) |
| 向已连接的端点发送数据 | |
| pair< ssize_t, ip_address > | receive_from (memory_view< char > buffer, int flags=0) |
| 接收数据报并获取发送方地址 | |
| ssize_t | receive (memory_view< char > buffer, int flags=0) |
| 从已连接的端点接收数据 | |
| Public 成员函数 继承自 ip_socket | |
| ip_socket ()=default | |
| 默认构造函数 | |
| ip_socket (const native_handle_type fd) noexcept | |
| 从原生句柄构造 | |
| ~ip_socket () override=default | |
| 析构函数 | |
| NEFORCE_NODISCARD int | address_family () const noexcept |
| 获取地址族 | |
| NEFORCE_NODISCARD bool | is_ipv4 () const noexcept |
| 检查是否为IPv4 socket | |
| NEFORCE_NODISCARD bool | is_ipv6 () const noexcept |
| 检查是否为IPv6 socket | |
| virtual void | connect (const ip_address &endpoint) |
| 连接到远程端点(TCP客户端) | |
| bool | close () noexcept override |
| 关闭socket | |
| Public 成员函数 继承自 socket_base | |
| socket_base () | |
| 默认构造函数 | |
| socket_base (const native_handle_type fd) noexcept | |
| 从原生句柄构造 | |
| socket_base (socket_base &&other) noexcept | |
| 移动构造函数 | |
| socket_base & | operator= (socket_base &&other) noexcept |
| 移动赋值运算符 | |
| virtual | ~socket_base () |
| 析构函数 | |
| NEFORCE_NODISCARD native_handle_type | native_handle () const noexcept |
| 获取原生句柄 | |
| NEFORCE_NODISCARD bool | is_open () const noexcept |
| 检查socket是否已打开 | |
| operator bool () const noexcept | |
| 布尔转换运算符 | |
| void | open (int family, int type, int protocol) |
| 打开socket | |
| bool | try_open (int family, int type, int protocol) noexcept |
| 尝试打开socket(不抛出异常) | |
| bool | set_nonblocking (bool enable) noexcept |
| 设置非阻塞模式 | |
| bool | shutdown_send () noexcept |
| 关闭发送方向 | |
| bool | shutdown_receive () noexcept |
| 关闭接收方向 | |
| bool | shutdown_both () noexcept |
| 关闭双向通信 | |
| bool | set_option (int level, int optname, const void *value, ::socklen_t len) noexcept |
| 设置socket选项 | |
| bool | get_option (int level, int optname, void *optval, ::socklen_t *optlen) const noexcept |
| 获取socket选项 | |
| bool | set_reuse_address (bool enable=true) noexcept |
| 设置地址重用(SO_REUSEADDR) | |
| bool | set_reuse_port (bool enable=true) noexcept |
| 设置端口重用(SO_REUSEPORT) | |
| bool | set_keep_alive (bool enable=true) noexcept |
| 设置TCP KeepAlive | |
| bool | set_tcp_nodelay (bool enable=true) noexcept |
| 设置TCP_NODELAY(禁用Nagle算法) | |
| bool | set_receive_buffer_size (int size) noexcept |
| 设置接收缓冲区大小 | |
| bool | set_send_buffer_size (int size) noexcept |
| 设置发送缓冲区大小 | |
| bool | set_send_timeout (milliseconds timeout) noexcept |
| 设置发送超时时间 | |
| bool | set_receive_timeout (milliseconds timeout) noexcept |
| 设置接收超时时间 | |
| NEFORCE_NODISCARD optional< ip_address > | local_endpoint () const |
| 获取本地端点地址 | |
| NEFORCE_NODISCARD optional< ip_address > | remote_endpoint () const |
| 获取远程端点地址 | |
| void | bind (const ip_address &endpoint) |
| 绑定socket到本地地址 | |
| void | listen (int backlog) |
| 开始监听连接(TCP) | |
| NEFORCE_NODISCARD native_handle_type | release () noexcept |
| 释放socket所有权 | |
额外继承的成员函数 | |
| Public 类型 继承自 socket_base | |
| using | native_handle_type |
| 平台原生句柄类型 | |
| 静态 Public 属性 继承自 socket_base | |
| static constexpr native_handle_type | invalid_handle |
| 无效句柄常量 | |
| Protected 属性 继承自 ip_socket | |
| int | family_ = AF_UNSPEC |
| 地址族 | |
| Protected 属性 继承自 socket_base | |
| native_handle_type | fd_ = invalid_handle |
| Socket句柄 | |
UDP Socket类
实现UDP协议的数据报socket,支持无连接和已连接两种模式。 UDP是面向数据报的协议,提供不可靠、无连接的服务。
主要功能:
在文件 udp_socket.hpp 第 38 行定义.
| void udp_socket::open | ( | int | family = AF_INET | ) |
打开UDP socket
| family | 地址族(AF_INET或AF_INET6),默认IPv4 |
| socket_exception | 创建失败时抛出 |
创建UDP协议的socket,使用SOCK_DGRAM类型和IPPROTO_UDP协议。
| ssize_t udp_socket::receive | ( | memory_view< char > | buffer, |
| int | flags = 0 ) |
从已连接的端点接收数据
| buffer | 接收缓冲区 |
| flags | 接收标志(通常为0) |
| socket_exception | 接收失败时抛出 |
| value_exception | socket未打开或缓冲区为空时抛出 |
从connect()关联的远程端点接收数据,需要先调用connect()。 只能接收来自该端点的数据。
| pair< ssize_t, ip_address > udp_socket::receive_from | ( | memory_view< char > | buffer, |
| int | flags = 0 ) |
接收数据报并获取发送方地址
| buffer | 接收缓冲区 |
| flags | 接收标志(通常为0) |
| socket_exception | 接收失败时抛出 |
| value_exception | socket未打开或缓冲区为空时抛出 |
接收UDP数据报,同时返回发送方的IP地址和端口。 适用于无连接模式的UDP服务器。
| ssize_t udp_socket::send | ( | memory_view< const char > | data, |
| int | flags = 0 ) |
向已连接的端点发送数据
| data | 要发送的数据 |
| flags | 发送标志(通常为0) |
| socket_exception | 发送失败时抛出 |
| value_exception | socket未打开时抛出 |
向connect()关联的远程端点发送数据,需要先调用connect()。 性能优于send_to(),因为不需要每次指定目标地址。
引用了 data().
| ssize_t udp_socket::send_to | ( | memory_view< const char > | data, |
| const ip_address & | endpoint, | ||
| int | flags = 0 ) |
向指定端点发送数据报
| data | 要发送的数据 |
| endpoint | 目标端点地址 |
| flags | 发送标志(通常为0) |
| socket_exception | 发送失败时抛出 |
| value_exception | socket未打开或端点无效时抛出 |
向指定的远程端点发送UDP数据报,不需要socket处于连接状态。
引用了 data().