EthernetClient Sample - Ambiguous overload error with latest Arduino 1.0.6

Hi,

Porting a webserver application I wrote to 1.0.6 and Im getting error accessing the Ethernet Class:

Ive gone back to the demo on the EthernetClient documentation - and even this wont work with the same error.

Trying to compile the demo code on this page:

Example

#include <Ethernet.h>
#include <SPI.h>

// the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
//the IP address for the shield:
byte ip[] = { 10, 0, 0, 177 };    
// the router's gateway address:
byte gateway[] = { 10, 0, 0, 1 };
// the subnet:
byte subnet[] = { 255, 255, 0, 0 };


// telnet defaults to port 23
EthernetServer server = EthernetServer(23);

void setup()
{
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);

  // start listening for clients
  server.begin();
}

void loop()
{
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client == true) {
    // read bytes from the incoming client and write them back
    // to any clients connected to the server:
    server.write(client.read());
  }
}

Gives me the following error

sketch_nov13a.ino: In function 'void loop()':
sketch_nov13a:30: error: ambiguous overload for 'operator==' in 'client == 1'
sketch_nov13a.ino:30: note: candidates are: operator==(int, int) <built-in>
C:\SDK\Arduino\IDE\libraries\Ethernet/EthernetClient.h:27: note:                 virtual bool EthernetClient::operator==(const EthernetClient&)

Seems you cannot do

  if (client == true)

anymore

Is there a new way of doing this and the samples havn't been updated?

Also - another issue with the same compiler..
Im getting an error setting a String to null with this compiler that wasnt an issue before

code:

String aString == NULL;

Compiler says:

sketch_nov13a:17: error: conversion from 'int' to 'String' is ambiguous

Easy fix is to set it to "" instead - but can you not set a String to null anymore?
Previously I Used 1.0.5 and all the above compiled just fine in 1.0.5

Thanks :slight_smile:

This works.

if(client) {

This is not correct.

  // change this
Ethernet.begin(mac, ip, gateway, subnet);
// to this
Ethernet.begin(mac, ip, gateway, gateway, subnet);

Im getting an error setting a String to null with this compiler that wasnt an issue before

That statement is NOT assigning a value to the String object (which you should not be using!). It is testing that the new String instance is NULL, which makes NO sense.

Hi -

Any update on this - the code I posted above IS NOT MINE - its the official Arduino sample code.
(My code is based on this)

This sample will not compile:

And same method I used mentioned is mentioned in several books and several ethernet sources - but it will not compile anymore on 1.0.6

(I have tried official 1.0.6 and the nightly build ARDUINO 1.6.0rc3 - 2015.02.03)

The problem is EthernetClient cannot be tested for true/false anymore (it used to have an override that allowed you to query it being ready using boolean operator).

The sample code have not worked since 1.0.5

  EthernetClient client = server.available();
  if (client == true) {
  }

What is the correct new way to do this test now - and will the documentation or the IDE/Compiler be changed back?

Thanks very much.

:slight_smile:

One more time...this works.

  EthernetClient client = server.available();
  if (client) {

Simple client test code you can try to see if it will compile and work.

//zoomkat 11-04-13
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test client GET
//for use with W5100 based ethernet shields
//remove SD card if inserted
//data from myIP server captured in readString 

#include <SPI.h>
#include <Ethernet.h>
String readString;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "checkip.dyndns.com"; // myIP server test web page server
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("client readString test 11/04/13"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
  Serial.println();
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET / HTTP/1.1"); //download text
    client.println("Host: checkip.dyndns.com");
    client.println("Connection: close");  //close 1.1 persistent connection  
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    readString += c; //places captured byte in readString
  }

  //Serial.println();
  client.stop(); //stop client
  Serial.println("client disconnected.");
  Serial.println("Data from server captured in readString:");
  Serial.println();
  Serial.print(readString); //prints readString to serial monitor 
  Serial.println();  
  Serial.println();
  Serial.println("End of readString");
  Serial.println("==================");
  Serial.println();
  readString=""; //clear readString variable

}

The EthernetClient library implements different things for operator== and the boolean conversion operator (bad for logical flow).

So in reality, if(client == true) is not the same as if(client)

The comparison operator (==) expects another EthernetClient on the RHS to check if two clients have the same socket.

The conversion operator returns true if the socket of the client you are looking at is valid. This is implicitly called when using if(client).

pYro_65:
The EthernetClient library implements different things for operator== and the boolean conversion operator (bad for logical flow).

So in reality, if(client == true) is not the same as if(client)

The comparison operator (==) expects another EthernetClient on the RHS to check if two clients have the same socket.

The conversion operator returns true if the socket of the client you are looking at is valid. This is implicitly called when using if(client).

Sounds like a perfect example of how NOT to implement a custom operator.... Nobody would guess that's what it actually did.

Regards,
Ray L.