Ethernet Library Bug :: No Repeated Connections...

While working with the Ethernet Shield, I found an issue with the Ethernet Library, that prevents repeated connections with a host.

Most hosts expect new TCP connections to originate from a different source port number. The suggested fix is below.

In Client.h, change as follows:

#ifndef Client_h
#define Client_h

#include "Print.h"

class Client : public Print {
private:
  uint8_t _sock;
  uint8_t *_ip;
  uint16_t _port;
  // 08-11-28 PW: Added 
  uint16_t _srcport;

In Client.cpp, change as follows:

extern "C" {
  #include "types.h"
  #include "w5100.h"
  #include "socket.h"
}

#include "Ethernet.h"
#include "Client.h"
#include "Server.h"

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

Client::Client(uint8_t *ip, uint16_t port) {
  _ip = ip;
  _port = port;
  // 08-11-28 PW: Initialize source port
  _srcport = 0;  
}

uint8_t Client::connect() {
  _sock = 255;
  
  for (int i = 0; i < MAX_SOCK_NUM; i++) {
    if (getSn_SR(i) == SOCK_CLOSED) {
      _sock = i;
    }
  }
  
  if (_sock == 255)
    return 0;

  // 08-11-28 PW: Update source port
  _srcport++;
    
  // 08-11-28 PW: Use different source port each connection
  socket(_sock, Sn_MR_TCP, (_srcport + 1024), 0);
  
  if (!::connect(_sock, _ip, _port))
    return 0;
    
  while (status() != SOCK_ESTABLISHED) {
    if (status() == SOCK_CLOSED)
      return 0;
  }
  
  return 1;
}

Cheers,

Paul...

It works finally. I get lots of data from Google. It has been a long haul brother, thanks for sticking with me.

My next magic trick is to try to make some sense of the data. Thanks so very much. I appreciate your help sincerely.

Hank :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:

Good to hear hank. Good luck! :smiley:

has anyone got this to work with arduino 13? I'm still having issues using pachube. It updates the first time but then won't connect again there after :frowning:

Actually, as I understand it the patch wasn't applied properly in Arduino 0013, so the library still needs to have part of the above fix applied.

I believe this line in Client.cpp:

socket(_sock, Sn_MR_TCP, _port, 1024 + _srcport);

needs to be replaced with this line:

socket(_sock, Sn_MR_TCP, 1024 + _srcport, 0);

--Phil.

I'm using v15 of the arduino system and I'm also seeing this bug.

I'm connecting out from my wiznet shield to a local apache2 server and when I hard reset the boards, the first connect always works. then I can do some more connects but not reliably. when it gets in this state, it still responds to loop() events (I have an IR receiver that detects remote keypresses and that still works fine) but outbound connects just fail. then I reset the board and all is fine for the next one, etc.

is v15 still in this broken state?

update: ok, v16 seems to work. yay!!

it was not easy, though :wink: I tried freebsd and could not build the IDE. tried linux (gentoo) and could not get avr tools to work. tried ubuntu - yay, that worked. but I do my work on winblows (usb serial and all that) so I wanted to try building the ide from win source. had to edit the make.sh file a lot (almost all commented-out things had to be uncommented for cygwin). finally I got an ide that said v16, I loaded my ethernet web client source, built it and it works great now! no more 'lost packets' (lol).

ethernet now is a LOT better. before, well, it was useless.

See here:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238640832

These enhancements are planned to be rolled into the next Arduino IDE version.