Suggested amendment to Ethernet Library

Hi all,

I have a sketch that connects to a server at boot time and remains connected. If the connection is dropped, the Arduino is automatically rebooted and the connection is re-established.

I struck a problem where the source port was always starting at 1024 (after looking through the new Client.cpp in 0021 I believe it may now always start at 1025). Some firewalls I’ve tested this device with take a while before they drop the state for that particular client IP and that particular client source port, which can cause a re-connection to fail until the state has been dropped by the firewall.

I made a minor amendment to Client.cpp and Client.h which optionally allows a srcportOffset to be specified in the constructor. By using a random number as the offset I was able to eliminate the problem.

My original amendment was for 0018, so I have just written it for 0021 but it is untested on an actual board (compiles fine however).

The amendment is fully backward compatible, and I was wondering if it would be possible to have it added to the official library if people see value in it?

My amendments to Client.h:
Before:

Client();
Client(uint8_t);
Client(uint8_t *, uint16_t);

After:

Client();
Client(uint8_t);
Client(uint8_t *, uint16_t);
Client(uint8_t *, uint16_t, uint16_t);

My amendments to Client.cpp:
Before:

Client::Client(uint8_t sock) : _sock(sock) {
}

Client::Client(uint8_t *ip, uint16_t port) : _ip(ip), _port(port), _sock(MAX_SOCK_NUM) {
}

After:

Client::Client(uint8_t sock) : _sock(sock) {
}

Client::Client(uint8_t *ip, uint16_t port) : _ip(ip), _port(port), _sock(MAX_SOCK_NUM) {
}

Client::Client(uint8_t *ip, uint16_t port, uint16_t srcportOffset) {
  _ip = ip;
  _port = port;
  _sock = MAX_SOCK_NUM;
  _srcport += srcportOffset;
  if(_srcport < 1024) {
    _srcport = 1024;
  }
}

Enjoy!