Problem with NativeEthernet post data to IIS (Asp.NET Core)

Harry888

Member
Hello everyone,

I'm using NativeEthernet as weblient to post data to IIS.
It works great as long as the length of data is less than 2'019 characters.
If the string is longer than 2'019 characters the httpRequest() function hang.

To be sure that IIS doesn't have a problem. I sent a string with a length of 7'200 characters from Postman. It succeeds.

To simulate this behavior, just increase the DATA_LEN const.
The host char array is also wrong

here is the code:
===========================================================

#include <NativeEthernet.h>

uint8_t mac[6];
void teensyMAC(uint8_t *mac);

// initialize the library instance:
EthernetClient client;

char host[] = "myDomain.com";

unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds

constexpr int DATA_LEN = 2'020;
char data[DATA_LEN];

void setup()
{
data[0] = '"';
for(int i = 1; i<DATA_LEN-2; ++i)
data = 'a';
data[DATA_LEN-2] = '"';
data[DATA_LEN-1] = '\0';

teensyMAC(mac);
Serial.begin(9600);
while (!Serial) { ; }

// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
}

void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.write(c);
}

// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}

// this method makes a HTTP connection to the server:
void httpRequest()
{
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();

// if there's a successful connection:
Serial.printf("%s %d\n %s\n", "Data-Len: ", strlen(data), data);
if (client.connect(host, 443, true))
{
Serial.println("connecting...");
// send the HTTP GET request:
client.printf("%s %s HTTP/1.0\r\n", "POST", "/api/Data/Test");
client.printf("Host: %s\r\n", host);
client.println("Connection: close");
client.println("Accept: text/plain");
client.println("Content-type: application/json; charset=utf-8");
client.printf("Content-length: %d\r\n", strlen(data));
client.println();
client.print(data);

// note the time that the connection was made:
lastConnectionTime = millis();
} else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}

void teensyMAC(uint8_t *mac)
{
for(uint8_t by=0; by<2; by++) mac[by]=(HW_OCOTP_MAC1 >> ((1-by)*8)) & 0xFF;
for(uint8_t by=0; by<4; by++) mac[by+2]=(HW_OCOTP_MAC0 >> ((3-by)*8)) & 0xFF;
Serial.printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}

===========================================================

Can you please give me a hint.

Thank you in advance

Harry
 
It's been a while since I configured NativeEthernet, but I think the default socket size for NativeEthernet is 2048, which seems suspiciously close to your limitation.

NativeEthernet.h

You could increase that to some bigger value before Ethernet.begin, eg:

Code:
Ethernet.setSocketSize(4096);

You may also be able to call client.print with smaller chunks of data, each less than the size of the buffer, but I haven't tested that? If that works, it would be more optimal.

I'd also suggest looking at using QNEthernet, as NativeEthernet was abandoned by it's original author years ago soon after release, but QNEthernet appears actively maintained. I am stuck with NativeEthernet for now as I need TLS sockets....
 
Last edited:
Thank you beermat !
your second suggestion (smaller chunks ...) works perfectly.

The QNEthernet solution is not yet fully developed. It seems like there is no working SSL client example.

Thank you
 
The QNEthernet solution is not yet fully developed. It seems like there is no working SSL client example.

There is a TLS example (note that I’m using “TLS” instead of “SSL” here because SSL is obsolete) that works for both clients and servers, but I admit getting it up and running for either a client or server is a little complex. See the MbedTLSDemo plus the Readme. I’ve used basically something similar for working code.

I’m working on doing a few things in this area:
1. Exploring how to get Mbed TLS 3.x working (instead of 2.x), and
2. More and hopefully simpler examples.

I have no timeline on this, however. It depends on my free time.
 
Back
Top