Mbed OS 文档翻译 之 参考(API(网络套接字(TCPServer)))

TCPServer

                                                                                

                                                                         TCPServer 类层次结构

TCPServer 类提供接受传入 TCP 连接的能力。listen 成员函数将服务器设置为侦听传入连接,accept 成员函数在传入连接上设置有状态 TCPSocket 实例。

构造函数接受 NetworkStack 指针以打开指定 NetworkInterface 上的套接字。如果未在构造函数中传递 NetworkStack 指针,则必须调用 open 以初始化套接字。

有关 TCP connect,send 和 recv API 的信息,请参阅 TCPSocket 类。

TCPServer 类参考

TCPServer 类参考

公共成员函数
  TCPServer ()
template
  TCPServer (S *stack)
virtual  ~TCPServer ()
nsapi_error_t  accept (TCPSocket *connection, SocketAddress *address=NULL)
 公共成员函数继承自 TCPSocket
  TCPSocket ()
template
  TCPSocket (S *stack)
virtual  ~TCPSocket ()
virtual int  join_multicast_group (const SocketAddress &address)
nsapi_error_t  connect (const char *host, uint16_t port)
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 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 TCPSocket *  accept (nsapi_error_t *error=NULL)
virtual nsapi_error_t  listen (int backlog=1)
 公共成员函数继承自 InternetSocket
virtual  ~InternetSocket ()
nsapi_error_t  open (NetworkStack *stack)
template
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
void  attach (T *obj, M method)
 公共成员函数继承自 Socket
virtual  ~Socket ()
其他继承成员
 受保护的成员函数继承自 TCPSocket
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

TCPServer 示例

这是一个 TCP 服务器示例。它接受单个 telnet 客户端连接并发送缓冲区数据。

main.cpp                                                                                                                                              导入到 Mbed IDE

#include "mbed.h"
#include "EthernetInterface.h"
#include "TCPServer.h"
#include "TCPSocket.h"

int main()
{
    printf("TCP server example\n");
    
    EthernetInterface eth;
    eth.connect();
    
    printf("The Server IP address is '%s'\n", eth.get_ip_address());
    
    TCPServer srv;
    TCPSocket client_sock;
    SocketAddress client_addr;
    char *buffer = new char[256];
    
    /* Open the server on ethernet stack */
    srv.open(ð);
    
    /* Bind the HTTP port (TCP 80) to the server */
    srv.bind(eth.get_ip_address(), 23);
    
    /* Can handle x simultaneous connections */
    srv.listen(1);

    srv.accept(&client_sock, &client_addr);
    printf("Accepted %s:%d\n", client_addr.get_ip_address(), 
                    client_addr.get_port());
    strcpy(buffer, "Hello \n\r");
    client_sock.send(buffer, strlen(buffer));
    client_sock.recv(buffer, 256);
    
    client_sock.close();
    delete[] buffer;
}

  • TCPSocket API 参考。

你可能感兴趣的:(Mbed,OS)