How to make the Client class try new connections?

Hi All,

I've had my Arduino a few weeks now, and my first project (an IR UPnP controller) is working really well.

I have, however, run up against a snag that I can't see my way round, and would be grateful for any tips.

I am creating a client and connecting to an IP address:

Client client (ip, port); if (client.connect ()) { /* do some stuff */ }

But what I want to do is try a list of IP addresses, so if the connection to the first address fails, I then try to connect to the second, then the third etc until a connection is established.

The problem is that it doesn't seem to be possible to change the IP address of a Client after the constructor has been called. There are no public functions in the class to allow the IP to be changed after creation; the Arduino language doesn't seem to support "new" to dynamically instantiate classes, and the compiler (quite rightly) objects if I explictly call the Client destructor and then call the constructor again with the same instance name. The IP address is private within the Client class, so I can't even subclass it to create a version with a public member function to access it.

The only thing I can see that will work is to create my own version of the Client class from scratch, in which I add an update_ip() public member function. This seems a very clunky way of doing things - as an Arduino newcomer, is there some other approach that I am missing?

Many thanks in advance for any assistance.

Were this my problem, I would simply edit the Client.h and Client.cpp files to add the functionality I desired.

After thorough testing, I’d offer the revisions back to the Arduino team to incorporate.

It isn’t really necessary to start from scratch.

What about something like this:

Instead of a global Client, make a function using a local client that is created/destroyed in a function.

Set I


byte ip1[] = {10,10,10,1};
byte ip2[] = {};

void loop()
    byte *ip;
    // set IP to first IP address here.
    ip = ip1;

         // set ip to next ip address in list
         // for example:
         ip = ip2;

bool Connect(byte* ip)
    Client client(ip, 80);
       // do stuff here with connection, it is connected. 
      return true;
      return false;

D'oh! Of course that'll work - I'm kicking myself very hard for not seeing it! I was trying to split the creation of the Client out into a subfunction, but was getting confused by trying to return it to the top level rather than just performing all Client-related activities in the sub function.

I've hacked something similar into my code, and it certainly builds ok - no reason to suspect it won't work. Many thanks.