Skip to content

Commit f187f4d

Browse files
committed
ArduinoNetAPILibs client.read(buff, size) return value research
1 parent 465f493 commit f187f4d

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

ArduinoNetAPIDev.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,9 @@ virtual int read(uint8_t *buf, size_t size) = 0;
497497
```
498498
Note: `Stream` doesn't have `read(byte, size)` method so any access to `Client` as `Stream` will not use it.
499499

500-
Usually `read(byte)` is implemented using `read(buffer, size)`, which handles the receive buffer.
500+
Method `read()` to read one byte should return -1 if no data are available. In case of `read(buffer,size)`most libraries return 0 if no data are available, but some libraries, including the classic Ethernet library can return -1.
501+
502+
Usually `read()` is implemented using `read(buffer, size)`, which handles the receive buffer.
501503
```
502504
int WiFiClient::read() {
503505
uint8_t b;

ArduinoNetAPILibs.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,31 @@ All libraries have [remoteIP()](https://www.arduino.cc/reference/en/libraries/wi
277277

278278
(3) WiFiClient is alias for NetworkClient. EthernetClient is not defined
279279

280+
### `client.read(buff, size)` return value
281+
282+
There are differences for return value of `client.read(buff, size)`. According to Arduino documentation return -1 is "no data available" and 0 is "connection is closed". And this is how it is in BSD sockets where there is no other way to get the closed state. But most Arduino libraries return 0 if no data are available and some return -1 if the client is not valid.
283+
284+
Note: The Arduino documentation mixes `read()` with `read(buff, size)`. `read()` should definitely return -1 if no byte is available, because 0 is a valid returned value.
285+
286+
| library | class | no data available | connection is closed (1) |
287+
|---|---|:---:|:---:|
288+
|Ethernet |[EthernetClient][21] | -1 | 0 |
289+
|WiFi101 |[WiFiClient][24] | -1 | 0 |
290+
|WiFiNINA | [WiFiClient][23] | 0 | |
291+
|WiFiS3 |[WiFiClient][25] | 0 | |
292+
|Mbed SocketWrapper | [MbedClent][26]| -1 | |
293+
|C33 lwIpWrapper |[lwipClient][28] | -1 | |
294+
|ESP8266WiFi |[WiFiClient][30] | 0 | |
295+
|esp32 Network | [NetworkClient][32] | 0 | |
296+
|WiFiEspAT|[WiFiClient][34] | 0 | |
297+
|EthernetENC |[EthernetClient][35] | 0 | -1 |
298+
|STM32Ethernet| [EthernetClient][36] | 0 | -1 |
299+
|QNEthernet |[EthernetClient][37] | 0 | 0 |
300+
|RP2040 WiFi | [WiFiClient][38] (2) | 0 | |
301+
302+
(1) empty field means that the state of the connection is not checked in read(buff, size)
303+
304+
280305
### Secure layer
281306

282307
| library | secure client | lastError() | setInsecure | allowSelfSignedCerts | disableSNI |

0 commit comments

Comments
 (0)