EthernetClient.setClientTimeout(timeout) looking for useage example

Philgaskin's modded Ethernet library GitHub - per1234/EthernetMod: Modified Arduino Ethernet Library has the ability to setClientTimeout duration. Looked for example/s in the his library; did not see any for using this feature.

If I understand the feature correctly; it allows you to set the timeout duration of the Ethernet Client.

I am using his modded library to obtain the remote client's ipaddress. This feature works well and compiled without issue. EthernetClient.setTimeout(timeout) feature is not compiling; error produced is:
"error: expected unqualified-id before '.' token."

~Three minute "Youtube" video of my project: - YouTube

Thank you.
William

EthernetClient.setTimeout(timeout) feature is not compiling; error produced is:
"error: expected unqualified-id before '.' token."

You have an instance of the EthernetClient class. It is that instance that you call the setTimeout() method for.

@PaulS

More detail please. Okay on having the Ethernet Client class instance.

Could you give an example of calling the setTimeout() method?

William

A Google search turned up this example of someone solving a problem by setting the timeout:

http://forum.arduino.cc/index.php?topic=117177.0

@ johnwasser

Thank you John! I must define my "Google" searches better!

Appreciate your input!

William

Hi Techno500 that is actually my library. Philgaskin shared the code for the remoteIP() function here on the forum so that is why I thanked him in the documentation but I added EthernetClient.setClientTimeout(). The reason I added this function is that the stock Arduino Ethernet library just has a hard coded 1000ms timeout on EthernetClient.stop() and no timeout at all on EthernetClient.connect(). It can often be useful to have control of the timeout especially if you are using a watchdog reset where of course you would want stop() to timeout before the watchdog timeout. Note that setClientTimeout() has a different purpose than setTimeout() which controls the timeout for the Stream functions. Here is an modified version of the ChatServer example that demonstrates of the use of setClientTimeout():

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

unsigned long sendTimeStamp;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);

IPAddress sendip(192, 168, 69, 100);

// telnet defaults to port 23
EthernetServer server(1024);
EthernetClient client;
boolean alreadyConnected = false; // whether or not the client was connected previously

void setup() {
  // initialize the ethernet device
  Ethernet.begin(mac, ip);//, gateway, subnet);
  // start listening for clients
  server.begin();
  //set the timeout for client.connect() and client.stop()
  client.setClientTimeout(100);
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Chat server address:");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // wait for a new client:
  
  EthernetClient connectedClient = server.available();

  // when the client sends the first byte, say hello:
  if (connectedClient) {
    if (!alreadyConnected) {
      // clead out the input buffer:
      connectedClient.flush();
      Serial.println("We have a new client");
      Serial.print("client IP address: ");
      Serial.println(connectedClient.remoteIP());
      connectedClient.println("Hello, client!");
      alreadyConnected = true;
    }

    if (connectedClient.available() > 0) {
      // read the bytes incoming from the client:
      char thisChar = connectedClient.read();
      // echo the bytes back to the client:
      server.write(thisChar);
      // echo the bytes to the server as well:
      Serial.write(thisChar);
    }
  }

  if (millis() - sendTimeStamp > 4000) {
    sendTimeStamp = millis();
    if (client.connect(sendip, 1024)) {
      Serial.println("send: connected");
      client.println("hi");
      client.stop();
    }
    else {
      Serial.println("send: connection failed");
    }
  }
}

This compiles for me using the Arduino-IDE-1.5plus and W5x00 branches of EthernetMod using Arduino IDE 1.6.5r2 and 1.6.6 2015/08/20 05:41. I'd be happy to look into your error message if this example doesn't fix it: please let me know what branch of EthernetMod you are using, what version of the Arduino IDE and some example code that causes the error. I hope you are finding my library useful and if you find any bugs please feel free to submit an issue on github(or contact me here if you don't have a github account).

@ pert

Thank you for taking time to reply to this topic; it is appreciated. Your library is working correctly with my project.

My project is posted: Tech500 (William Lucid) · GitHub

This is where I obtained your library: GitHub - per1234/EthernetMod: Modified Arduino Ethernet Library

William

@ Pert

Is the Adafruit_CC3000 library capable of using both Philgaskin and your modifications? I would like to be able to use remoteIP() and EthernetClient.setClientTimeout(timeout).

Adafruit_CC3000 library:

William

What timeout value do you think you are modifying? The link to the library mod above changes the connection close timeout only. It doesn't do anything about the establish connection timeout. I've always been able to modify the establish connection retries and timeout values.

The remote IP no longer needs a library modification unless you use the Wiznet library downloaded from Github, and that is easy. The Arduino crew has added a function to EthernetClient.cpp that allows you to get the current client socket number. With that socket number, you can get any info about the client, including the IP.

I don't know about the CC3000.

Techno500:
Is the Adafruit_CC3000 library capable of using both Philgaskin and your modifications?

No, it's a very different library.

Techno500:
EthernetClient.setClientTimeout(timeout).

There are some timeouts set in Adafruit_CC3000.h lines 55 and 56: Adafruit_CC3000_Library/Adafruit_CC3000.h at master · adafruit/Adafruit_CC3000_Library · GitHub
So you could edit that file to change those timeouts or it would be fairly easy to add functions to the library to allow the timeout values to be set via the sketch. I haven't looked closely enough at the code to see if there are other places where timeouts might be useful. You should also be able to use client.setTimeout() to set the timeout on the Stream functions.

SurferTim:
The remote IP no longer needs a library modification unless you use the Wiznet library downloaded from Github, and that is easy. The Arduino crew has added a function to EthernetClient.cpp that allows you to get the current client socket number. With that socket number, you can get any info about the client, including the IP.

I don't know about the CC3000.

@ SurferTim Would you have an example of using the current socket number finding the remoteIP?

William

This is the basic addition to any server code. It retrieves and prints the socket and IP.

#include <utility/w5100.h>

byte thisSocket;
byte remoteIP[4];

  EthernetClient client = server.available();
  if(client) {
    // get the socket number
    thisSocket = client.getSocketNumber();

    // print the socket number
    Serial.print(F("socket "));
    Serial.print(thisSocket);
    Serial.print(F(": "));

    // use the socket number to get the remote IP
    W5100.readSnDIPR(thisSocket,remoteIP);

    // print the remote IP
    Serial.print(remoteIP[0]);
    Serial.print(".");
    Serial.print(remoteIP[1]);
    Serial.print(".");
    Serial.print(remoteIP[2]);
    Serial.print(".");
    Serial.println(remoteIP[3]);

edit: IDE V1.6.6 or newer. Earlier versions and Wiznet Github library require minor mods.

Thank you SurferTim!

Also, posted question "Find remote IP TCP" on arduino.stackexchange.com; received reply from TisteAndii regarding modification of the Adafruit CC3000 library.

For reference the link to "Find remote IP TCP" and TisteAndii's post:

William

Find remote ip address TCP - Arduino Stack Exchange.pdf (164 KB)