UDPSocket
UDPSocket 类层次结构
UDPSocket 类提供了使用 sendto 和 recvfrom 成员函数通过 UDP 发送数据包的功能。数据包可能丢失或无序到达,因此我们建议您在需要保证交付时使用 TCPSocket。
构造函数接受 NetworkStack 指针以打开指定 NetworkInterface 上的套接字。如果你没有传入构造函数,那么你必须调用 open 来初始化套接字。
UDPSocket 类参考
公共成员函数 | |
UDPSocket () | |
template<typename S > | |
UDPSocket (S *stack) | |
virtual | ~UDPSocket () |
virtual nsapi_size_or_error_t | sendto (const char *host, uint16_t port, const void *data, nsapi_size_t size) |
virtual nsapi_size_or_error_t | sendto (const SocketAddress &address, const void *data, nsapi_size_t size) |
virtual nsapi_size_or_error_t | recvfrom (SocketAddress *address, void *data, nsapi_size_t size) |
virtual nsapi_error_t | connect (const SocketAddress &address) |
virtual nsapi_size_or_error_t | send (const void *data, nsapi_size_t size) |
virtual nsapi_size_or_error_t | recv (void *data, nsapi_size_t size) |
virtual Socket * | accept (nsapi_error_t *error=NULL) |
virtual nsapi_error_t | listen (int backlog=1) |
公共成员函数继承自 InternetSocket | |
virtual | ~InternetSocket () |
nsapi_error_t | open (NetworkStack *stack) |
template<typename S > | |
nsapi_error_t | open (S *stack) |
virtual nsapi_error_t | close () |
int | join_multicast_group (const SocketAddress &address) |
int | leave_multicast_group (const SocketAddress &address) |
nsapi_error_t | bind (uint16_t port) |
nsapi_error_t | bind (const char *address, uint16_t port) |
virtual nsapi_error_t | bind (const SocketAddress &address) |
virtual void | set_blocking (bool blocking) |
virtual void | set_timeout (int timeout) |
virtual nsapi_error_t | setsockopt (int level, int optname, const void *optval, unsigned optlen) |
virtual nsapi_error_t | getsockopt (int level, int optname, void *optval, unsigned *optlen) |
virtual void | sigio (mbed::Callback< void()> func) |
void | attach (mbed::Callback< void()> func) |
template<typename T , typename M > | |
void | attach (T *obj, M method) |
公共成员函数继承自 Socket | |
virtual | ~Socket () |
受保护的成员函数 | |
virtual nsapi_protocol_t | get_proto () |
受保护的成员函数继承自 InternetSocket | |
virtual void | event () |
int | modify_multicast_group (const SocketAddress &address, nsapi_socket_option_t socketopt) |
其他继承成员 | |
受保护的属性继承自 InternetSocket | |
NetworkStack * | _stack |
nsapi_socket_t | _socket |
uint32_t | _timeout |
mbed::Callback< void()> | _event |
mbed::Callback< void()> | _callback |
rtos::EventFlags | _event_flag |
rtos::Mutex | _lock |
SocketAddress | _remote_peer |
uint8_t | _readers |
uint8_t | _writers |
volatile unsigned | _pending |
bool | _factory_allocated |
从中继承的静态保护属性 InternetSocket | |
static const int | READ_FLAG = 0x1u |
static const int | WRITE_FLAG = 0x2u |
static const int | FINISHED_FLAG = 0x3u |
UDPSocket 示例
以下是通过向 NIST Internet 时间服务发送请求来读取当前 UTC 时间的 UDP 示例。
#include "mbed.h"
#include "EthernetInterface.h"
// Network interface
EthernetInterface net;
// Time protocol implementation : Address: time.nist.gov UDPPort: 37
typedef struct {
uint32_t secs; // Transmit Time-stamp seconds.
}ntp_packet;
int main() {
// Bring up the ethernet interface
printf("UDP Socket example\n");
if(0 != net.connect()) {
printf("Error connecting\n");
return -1;
}
// Show the network address
const char *ip = net.get_ip_address();
printf("IP address is: %s\n", ip ? ip : "No IP");
UDPSocket sock(&net);
SocketAddress sockAddr;
char out_buffer[] = "time";
if(0 > sock.sendto("time.nist.gov", 37, out_buffer, sizeof(out_buffer))) {
printf("Error sending data\n");
return -1;
}
ntp_packet in_data;
int n = sock.recvfrom(&sockAddr, &in_data, sizeof(ntp_packet));
in_data.secs = ntohl( in_data.secs ) - 2208988800; // 1900-1970
printf("Time Received %lu seconds since 1/01/1900 00:00 GMT\n",
(uint32_t)in_data.secs);
printf("Time = %s", ctime(( const time_t* )&in_data.secs));
printf("Time Server Address: %s Port: %d\n\r",
sockAddr.get_ip_address(), sockAddr.get_port());
// Close the socket and bring down the network interface
sock.close();
net.disconnect();
return 0;
}
相关内容
- TCPSocket API 参考。