23template <
typename TcpServer,
typename TcpClient,
typename UdpSocket>
27 using TcpServerType = TcpServer;
28 using TcpClientType = TcpClient;
29 using UdpSocketType = UdpSocket;
34 TcpServerType *srv =
new TcpServerType(port);
46 static constexpr TcpClient* NULL_TCP_SOCKET =
nullptr;
47 static constexpr UdpSocket* NULL_UDP_SOCKET =
nullptr;
71 *addr = s->remoteIP();
72 *port = s->remotePort();
96 UdpSocket* s =
new UdpSocket();
98 if (!s->begin(portNum)) {
99 printf(
"Can't bind port %d\n", portNum);
114 static ssize_t
sendSocket(TcpClient* sockfd,
const void *buf,
size_t len) {
115 return sockfd->write((uint8_t *)buf, len);
127 static ssize_t
sendUdpSocket(UdpSocket* sockfd,
const void *buf,
size_t len,
128 IPAddress destaddr, uint16_t destport) {
129 sockfd->beginPacket(destaddr, destport);
130 sockfd->write((
const uint8_t *)buf, len);
131 if (!sockfd->endPacket()) printf(
"error sending udp packet\n");
144 static int readSocket(TcpClient* sock,
char *buf,
size_t buflen,
146 if (!sock->connected()) {
147 printf(
"client has closed the socket\n");
151 int numAvail = sock->available();
152 if (numAvail == 0 && timeoutmsec != 0) {
155 numAvail = sock->available();
163 int numRead = sock->readBytes(buf, buflen);