Go Down

Topic: Ethernet Library Bug :: No Repeated Connections... (Read 1 time) previous topic - next topic


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: [Select]
#ifndef Client_h
#define Client_h

#include "Print.h"

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

In Client.cpp, change as follows:
Code: [Select]
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
 // 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;




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 :) :) :) :) :) :) :) :) :) :)



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


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


Go Up