33template <
typename TcpServer,
typename TcpClient,
typename UdpSocket>
37 using TcpServerType = TcpServer;
38 using TcpClientType = TcpClient;
39 using UdpSocketType = UdpSocket;
44 TcpServerType *srv =
new TcpServerType(port);
56 static constexpr TcpClient* NULL_TCP_SOCKET =
nullptr;
57 static constexpr UdpSocket* NULL_UDP_SOCKET =
nullptr;
81 *addr = s->remoteIP();
82 *port = s->remotePort();
106 UdpSocket* s =
new UdpSocket();
108 if (!s->begin(portNum)) {
109 printf(
"Can't bind port %d\n", portNum);
124 static ssize_t
sendSocket(TcpClient* sockfd,
const void *buf,
size_t len) {
125 return sockfd->write((uint8_t *)buf, len);
137 static ssize_t
sendUdpSocket(UdpSocket* sockfd,
const void *buf,
size_t len,
138 IPAddress destaddr, uint16_t destport) {
139 sockfd->beginPacket(destaddr, destport);
140 sockfd->write((
const uint8_t *)buf, len);
141 if (!sockfd->endPacket()) printf(
"error sending udp packet\n");
154 static int readSocket(TcpClient* sock,
char *buf,
size_t buflen,
156 if (!sock->connected()) {
157 printf(
"client has closed the socket\n");
161 int numAvail = sock->available();
162 if (numAvail == 0 && timeoutmsec != 0) {
165 numAvail = sock->available();
173 int numRead = sock->readBytes(buf, buflen);