Go Down

Topic: Modify Client to allow IP address to be changed (Read 643 times) previous topic - next topic


Due to the way the Arduino handles classes (ie a bit limited - can't use "new" to instantiate, for example), the existing implementation of the Client class has a problem.

I had a situation where I had a list of possible hosts, and I needed to connect to one of them - didn't matter which one. What I wanted to be able to do was try to connect to each in turn, and if a connection failed, to move onto the next in the list. To do this with the current implementation of Client means declaring a separate client for each connection, as it is impossible to change the IP address outside of the constructor.

I have therefore added a method to the Client class, as follows:

uint8_t Client::address(uint8_t *ip) {
 if (_sock != 255)
   return 0;
 _ip = ip;
 return 1;

This allows the IP at which the Client is targeted to be changed, as long as it isn't currently connected.

Is this a sensible addition to the Client class? If so, could it be added?

Go Up