WiFiClient client = server.available(); fails

I lifted the code from Arduino LiveCast Series S02E04 ([livecast/Season 2/Episode 4 - MKR1010 WiFi setup at master · arduino/livecast · GitHub](https://github.com/arduino/livecast/tree/master/Season 2/Episode 4 - MKR1010 WiFi setup)). It didn't work.

From it, I created the MCVE (at the end), but in short, client never turns true.

void loop() {

   WiFiClient client = server.available();   // listen for incoming clients

  if (client) {

I use Arduino IDE 1.8.13 to produce code for the MKR WiFi 1010.

The compiler includes WiFiNINA.h@1.8.7.

The code successfully launches a DHCP request. It can retrieve network time. It successfully launches ICMP ping requests and gets responses. It responds to ping requests.

When I make an HTTP request to MKR (a TCP SYN packet), the MKR responds with [RST, ACK]. The ACK acknowledges the request, but the RST indicates a refusal, as I understand it.

SYN Request from my PC browser (192.168.1.7)

Frame 69335: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface \Device\NPF_{EEC68D41-9E1E-48C4-9634-E507FDD38690}, id
0
Ethernet II, Src: IntelCor_e1:b2:11 (60:36:dd:e1:b2:11), Dst: Espressi_91:11:94 (4c:11:ae:91:11:94)
Internet Protocol Version 4, Src: 192.168.1.7, Dst: 192.168.1.8
Transmission Control Protocol, Src Port: 56087, Dst Port: 80, Seq: 0, Len: 0

[RST, ACK] response from MKR (192.168.1.8)

Frame 69455: 54 bytes on wire (432 bits), 54 bytes captured (432 bits) on interface \Device\NPF_{EEC68D41-9E1E-48C4-9634-E507FDD38690}, id 0
Ethernet II, Src: Espressi_91:11:94 (4c:11:ae:91:11:94), Dst: IntelCor_e1:b2:11 (60:36:dd:e1:b2:11)
Internet Protocol Version 4, Src: 192.168.1.8, Dst: 192.168.1.7
Transmission Control Protocol, Src Port: 80, Dst Port: 56087, Seq: 1, Ack: 1, Len: 0

Frankly, I don't understand the man page or the logic behind using a WiFiClient class member to create a server, but when someone gives a pithy response, then I'll get it.

Sandbox200403.ino (9.06 KB)

server.available() only returns a client which has data available. the concept of WiFiServer/EthernetServer is that it manages the clients and makes them available to sketch only if they send data. The server object is capable of sending data to all clients with write and print functions.

this applies for other Arduino WiFi libraries and Ethernet libraries too. the Ethernet library and my networking libraries (WiFiEspAT and EthernetENC) have server.accept() to return a new connected client. WiFiNina doesn't have it.

note: the esp8266 and esp32 server.available() works as Arduino server.accept().

"The concept of WiFiServer/EthernetServer is that it manages the clients and makes them available to sketch only if they send data."

"server.available() only returns a client which has data available."

Thank you. I am not experienced in HTTP 1.1, so I need the simpleton explanation.

I understand my server instance to be properly configured. It offers up an open port 80, to which my Chrome or Edge browser sends a TCP [SYN] packet when I direct it to URL 192.168.1.8 (in this case). The browser is ignorant of what it will find on that port, so it sends no data. My server, finding no data available(), does not respond with the HTTP header and all stuff that follows.

Instead, the server issues a TCP [ACK, RST] response, which my browser interprets as "connection refused".

It seems to me that there is a missing step somewhere. Either (a) the server needs to pick up the phone and say "Hello," even if there is no voice on the line. Or (b) the browser needs to start talking immediately to get the server's attention and turn available() to true.

So went back to basics and reproduced the code in the man page. Success! The browser now fails with

[color=#5f6368][b]192.168.1.8[/b] didn’t send any data.[/color]

[color=#5f6368][color=var(--error-code-color)]ERR_EMPTY_RESPONSE[/color][/color]

My new sandbox sketch still #includes <WiFiNINA.h> Your response helped me discover a typo in the line " server.begin();"

Life is good. Many thanks.

[color=#5f6368]/**[/color]
[color=#5f6368]   @file       Sandbox[/color]
[color=#5f6368]   @brief      MVCE for WiFi server failure[/color]
[color=#5f6368]   @author     Robert Hadow[/color]
[color=#5f6368]   @date       April 2021[/color]
[color=#5f6368]   @par Revision History:[/color]
[color=#5f6368]**/[/color]
[color=#5f6368]
[/color]
[color=#5f6368]// compiled for MKR Wifi 1010[/color]
[color=#5f6368]// built with Arduino IDE 1.8.13[/color]
[color=#5f6368]
[/color]
[color=#5f6368]#include <WiFiNINA.h>  // ResolveLibrary(WiFiNINA.h) -> candidates: [WiFiNINA@1.8.7][/color]
[color=#5f6368]#include "arduino_secrets.h"[/color]
[color=#5f6368]
[/color]
[color=#5f6368]char ssid[] = SECRET_SSID;       // network SSID (name)[/color]
[color=#5f6368]char pass[] = SECRET_PASS;       // network password for WPA2[/color]
[color=#5f6368]int status = WL_IDLE_STATUS;     // used occasionally. better is WiFi.status()[/color]
[color=#5f6368]WiFiServer server(80);[/color]
[color=#5f6368]
[/color]
[color=#5f6368]
[/color]
[color=#5f6368]void setup() {[/color]
[color=#5f6368]  // initialize serial:[/color]
[color=#5f6368]  Serial.begin(9600);[/color]
[color=#5f6368]  Serial.println("Attempting to connect to WPA network...");[/color]
[color=#5f6368]  Serial.print("SSID: ");[/color]
[color=#5f6368]  Serial.println(ssid);[/color]
[color=#5f6368]
[/color]
[color=#5f6368]  status = WiFi.begin(ssid, pass);[/color]
[color=#5f6368]  if ( status != WL_CONNECTED) {[/color]
[color=#5f6368]    Serial.println("Couldn't get a wifi connection");[/color]
[color=#5f6368]    while(true);[/color]
[color=#5f6368]  }[/color]
[color=#5f6368]  else {[/color]
[color=#5f6368]    server.begin();[/color]
[color=#5f6368]    Serial.print("Connected to wifi. My address:");[/color]
[color=#5f6368]    IPAddress myAddress = WiFi.localIP();[/color]
[color=#5f6368]    Serial.println(myAddress);[/color]
[color=#5f6368]
[/color]
[color=#5f6368]  }[/color]
[color=#5f6368]}[/color]
[color=#5f6368]
[/color]
[color=#5f6368]void loop() {[/color]
[color=#5f6368]  // listen for incoming clients[/color]
[color=#5f6368]  WiFiClient client = server.available();[/color]
[color=#5f6368]  if (client) {[/color]
[color=#5f6368]
[/color]
[color=#5f6368]    if (client.connected()) {[/color]
[color=#5f6368]      Serial.println("Connected to client");[/color]
[color=#5f6368]  [/color]
[color=#5f6368]    }[/color]
[color=#5f6368]
[/color]
[color=#5f6368]    // close the connection:[/color]
[color=#5f6368]    client.stop();[/color]
[color=#5f6368]  }[/color]
[color=#5f6368]}[/color]

Something in my previous sketch interferes with this operation.

this has nothing to do with HTTP. WiFiClient is a TCP client. WiFiServer is a TCP server (plus the mentioned clients management).

on this level you must handle HTTP protocol in sketch

see the examples of the WiFiNina library in IDE Examples menu

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.