Pages: [1]   Go Down
Author Topic: Ethernet Library Bug :: No Repeated Connections...  (Read 1539 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
#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:
Code:
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...
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley smiley smiley smiley smiley smiley smiley smiley smiley smiley
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good to hear hank.  Good luck!  smiley-grin
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
I love YaBB 1G - SP1!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :-(
Logged

New Zealand
Offline Offline
God Member
*****
Karma: 0
Posts: 999
Arduino pebbles
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
socket(_sock, Sn_MR_TCP, _port, 1024 + _srcport);
needs to be replaced with this line:
Code:
socket(_sock, Sn_MR_TCP, 1024 + _srcport, 0);

--Phil.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 94
sercona audio
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 94
sercona audio
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

it was not easy, though smiley-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.

Logged

Indiana
Offline Offline
Full Member
***
Karma: 1
Posts: 234
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: