Skip to content

Commit 17d84b5

Browse files
committed
By choosing a receive buffer smaller than the total UDP payload size (in our examples) we can test the functionality of reading a UDP packet until exhaustion.
1 parent 738944e commit 17d84b5

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
lines changed

examples/UDP_Client/UDP_Client.ino

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ static T1SMacSettings const t1s_default_mac_settings;
2929
static IPAddress const UDP_SERVER_IP_ADDR = {192, 168, 42, 100 + 0};
3030
static uint16_t const UDP_CLIENT_PORT = 8889;
3131
static uint16_t const UDP_SERVER_PORT = 8888;
32-
static uint8_t udp_tx_msg_buf[256] = {0};
33-
static uint8_t udp_rx_msg_buf[256] = {0};
3432

3533
/**************************************************************************************
3634
* GLOBAL VARIABLES
@@ -152,21 +150,27 @@ void loop()
152150
Serial.print(udp_client.remotePort());
153151
Serial.println();
154152

155-
/* Read from received UDP packet. */
156-
int const bytes_read = udp_client.read(udp_rx_msg_buf, sizeof(udp_rx_msg_buf));
157-
if (bytes_read > 0) {
158-
udp_rx_msg_buf[bytes_read] = 0;
159-
}
160-
161-
/* Finish reading the current packet. */
162-
udp_client.flush();
163-
164153
Serial.print("[");
165154
Serial.print(millis());
166155
Serial.print("] UDP_Client received packet content: \"");
167-
Serial.print(reinterpret_cast<char *>(udp_rx_msg_buf));
156+
157+
/* Read from received UDP packet. */
158+
size_t const UDP_RX_MSG_BUF_SIZE = 16 + 1; /* Reserve the last byte for the '\0' termination. */
159+
uint8_t udp_rx_msg_buf[UDP_RX_MSG_BUF_SIZE] = {0};
160+
int bytes_read = udp_client.read(udp_rx_msg_buf, UDP_RX_MSG_BUF_SIZE - 1);
161+
while(bytes_read != 0)
162+
{
163+
/* Print received data to Serial. */
164+
udp_rx_msg_buf[bytes_read] = '\0'; /* Terminate buffer so that we can print it as a C-string. */
165+
Serial.print(reinterpret_cast<char *>(udp_rx_msg_buf));
166+
167+
/* Continue reading. */
168+
bytes_read = udp_client.read(udp_rx_msg_buf, UDP_RX_MSG_BUF_SIZE - 1);
169+
}
168170
Serial.println("\"");
169171

172+
/* Finish reading the current packet. */
173+
udp_client.flush();
170174
}
171175
}
172176

examples/UDP_Server/UDP_Server.ino

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ static T1SPlcaSettings const t1s_plca_settings{T1S_PLCA_NODE_ID};
3333
static T1SMacSettings const t1s_default_mac_settings;
3434

3535
static uint16_t const UDP_SERVER_LOCAL_PORT = 8888;
36-
static uint8_t udp_rx_msg_buf[256] = {0};
3736

3837
/**************************************************************************************
3938
* GLOBAL VARIABLES
@@ -119,34 +118,50 @@ void loop()
119118
}
120119

121120
/* Check for incoming UDP packets. */
122-
int const packet_size = udp_server.parsePacket();
123-
if (packet_size)
121+
int const rx_packet_size = udp_server.parsePacket();
122+
if (rx_packet_size)
124123
{
124+
std::vector<uint8_t> udp_tx_buf(rx_packet_size);
125+
IPAddress const destination_ip = udp_server.remoteIP();
126+
uint16_t const destination_port = udp_server.remotePort();
127+
125128
/* Print some metadata from received UDP packet. */
126129
Serial.print("Received ");
127-
Serial.print(packet_size);
130+
Serial.print(rx_packet_size);
128131
Serial.print(" bytes from ");
129132
Serial.print(udp_server.remoteIP());
130133
Serial.print(" port ");
131134
Serial.print(udp_server.remotePort());
132135
Serial.println();
133136

137+
Serial.print("[");
138+
Serial.print(millis());
139+
Serial.print("] UDP_Client received packet content: \"");
140+
134141
/* Read from received UDP packet. */
135-
int const bytes_read = udp_server.read(udp_rx_msg_buf, sizeof(udp_rx_msg_buf));
136-
if (bytes_read > 0) {
137-
udp_rx_msg_buf[bytes_read] = 0;
142+
size_t const UDP_RX_MSG_BUF_SIZE = 16 + 1; /* Reserve the last byte for the '\0' termination. */
143+
uint8_t udp_rx_msg_buf[UDP_RX_MSG_BUF_SIZE] = {0};
144+
int bytes_read = udp_server.read(udp_rx_msg_buf, UDP_RX_MSG_BUF_SIZE - 1);
145+
while(bytes_read != 0)
146+
{
147+
/* Copy received data into transmit buffer for echo functionality. */
148+
std::copy(udp_rx_msg_buf, udp_rx_msg_buf + bytes_read, std::back_inserter(udp_tx_buf));
149+
150+
/* Print received data to Serial. */
151+
udp_rx_msg_buf[bytes_read] = '\0'; /* Terminate buffer so that we can print it as a C-string. */
152+
Serial.print(reinterpret_cast<char *>(udp_rx_msg_buf));
153+
154+
/* Continue reading. */
155+
bytes_read = udp_server.read(udp_rx_msg_buf, UDP_RX_MSG_BUF_SIZE - 1);
138156
}
157+
Serial.println("\"");
139158

140159
/* Finish reading the current packet. */
141160
udp_server.flush();
142161

143-
Serial.print("UDP_Server received packet content: \"");
144-
Serial.print(reinterpret_cast<char *>(udp_rx_msg_buf));
145-
Serial.println("\"");
146-
147162
/* Send back a reply, to the IP address and port we got the packet from. */
148-
udp_server.beginPacket(udp_server.remoteIP(), udp_server.remotePort());
149-
udp_server.write((const uint8_t *)udp_rx_msg_buf, packet_size);
163+
udp_server.beginPacket(destination_ip, destination_port);
164+
udp_server.write(udp_tx_buf.data(), udp_tx_buf.size());
150165
udp_server.endPacket();
151166
}
152167
}

0 commit comments

Comments
 (0)