25template <
typename TcpServer,
typename TcpClient,
typename UdpSocket>
29 using TcpServerType = TcpServer;
30 using TcpClientType = TcpClient;
31 using UdpSocketType = UdpSocket;
36 TcpServerType *srv =
new TcpServerType(port);
48 static constexpr TcpClient* NULL_TCP_SOCKET =
nullptr;
49 static constexpr UdpSocket* NULL_UDP_SOCKET =
nullptr;
73 *addr = s->remoteIP();
74 *port = s->remotePort();
98 UdpSocket* s =
new UdpSocket();
100 if (!s->begin(portNum)) {
101 printf(
"Can't bind port %d\n", portNum);
116 static ssize_t
sendSocket(TcpClient* sockfd,
const void *buf,
size_t len) {
117 return sockfd->write((uint8_t *)buf, len);
129 static ssize_t
sendUdpSocket(UdpSocket* sockfd,
const void *buf,
size_t len,
130 IPAddress destaddr, uint16_t destport) {
131 sockfd->beginPacket(destaddr, destport);
132 sockfd->write((
const uint8_t *)buf, len);
133 if (!sockfd->endPacket()) printf(
"error sending udp packet\n");
146 static int readSocket(TcpClient* sock,
char *buf,
size_t buflen,
148 if (!sock->connected()) {
149 printf(
"client has closed the socket\n");
153 int numAvail = sock->available();
154 if (numAvail == 0 && timeoutmsec != 0) {
157 numAvail = sock->available();
165 int numRead = sock->readBytes(buf, buflen);