Mistake on this page?
Report an issue in GitHub or email us


TCPServer class hierarchy

The TCPServer class provides the ability to accept incoming TCP connections. The listen member function sets up the server to listen for incoming connections, and the accept member function sets up a stateful TCPSocket instance on an incoming connection.

The constructor takes in the NetworkStack pointer to open the socket on the specified NetworkInterface. If you do not pass the NetworkStack pointer in the constructor, then you must call open to initialize the socket.

Refer to TCPSocket class for TCP connect, send and recv APIs.

TCPServer class reference

Public Member Functions
 TCPServer ()
template<typename S >
 TCPServer (S *stack)
virtual ~TCPServer ()
nsapi_error_t listen (int backlog=1)
nsapi_error_t accept (TCPSocket *connection, SocketAddress *address=NULL)
 Public Member Functions inherited from Socket
virtual ~Socket ()
nsapi_error_t open (NetworkStack *stack)
template<typename S >
nsapi_error_t open (S *stack)
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)
nsapi_error_t bind (const SocketAddress &address)
void set_blocking (bool blocking)
void set_timeout (int timeout)
nsapi_error_t setsockopt (int level, int optname, const void *optval, unsigned optlen)
nsapi_error_t getsockopt (int level, int optname, void *optval, unsigned *optlen)
void sigio (mbed::Callback< void()> func)
void attach (mbed::Callback< void()> func)
template<typename T , typename M >
void attach (T *obj, M method)
Protected Member Functions
virtual nsapi_protocol_t get_proto ()
virtual void event ()
 Protected Member Functions inherited from Socket
int modify_multicast_group (const SocketAddress &address, nsapi_socket_option_t socketopt)
Protected Attributes
volatile unsigned _pending
rtos::Semaphore _accept_sem
 Protected Attributes inherited from Socket
nsapi_socket_t _socket
uint32_t _timeout
mbed::Callback< void()> _event
mbed::Callback< void()> _callback
rtos::Mutex _lock

TCPServer example

Here is a TCP server example. It accepts single telnet client connection and sends buffer data.

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

int main()
    printf("TCP server example\n");
    EthernetInterface eth;
    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 */
    /* Bind the HTTP port (TCP 80) to the server */
    srv.bind(eth.get_ip_address(), 23);
    /* Can handle x simultaneous connections */

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

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.