Skip to content

Commit 4000c91

Browse files
committed
Added new method to UDP to take a hostname rather than an IP address. Part of issue 243
1 parent a310cb8 commit 4000c91

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

libraries/Ethernet/Udp.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "socket.h"
3131
#include "Ethernet.h"
3232
#include "Udp.h"
33+
#include "Dns.h"
3334

3435
/* Constructor */
3536
UDP::UDP() : _sock(MAX_SOCK_NUM) {}
@@ -74,6 +75,22 @@ void UDP::stop()
7475
_sock = MAX_SOCK_NUM;
7576
}
7677

78+
int UDP::beginPacket(const char *host, uint16_t port)
79+
{
80+
// Look up the host first
81+
int ret = 0;
82+
DNSClient dns;
83+
IPAddress remote_addr;
84+
85+
dns.begin(Ethernet.dnsServerIP());
86+
ret = dns.getHostByName(host, remote_addr);
87+
if (ret == 1) {
88+
return beginPacket(remote_addr, port);
89+
} else {
90+
return ret;
91+
}
92+
}
93+
7794
int UDP::beginPacket(IPAddress ip, uint16_t port)
7895
{
7996
_offset = 0;

libraries/Ethernet/Udp.h

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class UDP : public Stream {
6060
// Start building up a packet to send to the remote host specific in ip and port
6161
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
6262
int beginPacket(IPAddress ip, uint16_t port);
63+
// Start building up a packet to send to the remote host specific in host and port
64+
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
65+
int beginPacket(const char *host, uint16_t port);
6366
// Finish off this packet and send it
6467
// Returns 1 if the packet was sent successfully, 0 if there was an error
6568
int endPacket();

0 commit comments

Comments
 (0)