Go Down

Topic: Bug in Ethernet Library in new Arduino 0013 (Read 3512 times) previous topic - next topic

MFV

hi!

There is a bug in the Arduino 0013 Ethernet library.

A second connection to the same server doesn´t work. Only the first connection will work. Exact same code works fine in Arduino 0012.
I need the new float feature in the 0013 Ethernet libarary. So it would be nice if you fix it or release a work around. Thanks.

MFV


mellis

Can you post your code?  What happens when you try to make the second connection?

Digger450

I am having the same issue.  It hangs the Arduino completely on the second connection attempt.  Here is the code I am using:

Code: [Select]
#include <Ethernet.h>

// network configuration.  gateway and subnet are optional.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 178 };
byte server[] = { 192, 168, 1, 54 };

Client client(server, 13000);

long timerX = 31000;

void setup()
{
 // initialize the ethernet device
 Ethernet.begin(mac, ip);
 Serial.begin(9600);
 Serial.println("Ethernet ChatServer Online");
}

void loop()
{
 if (client.available())
 {
   while (client.available())
   {
     char c = client.read();
     Serial.print(c);
   }
 }
 
 long currentTime = millis();

 if (abs(currentTime - timerX) > 10000)
 {
   Serial.println(timerX);
   if (client.connect())
   {
     client.println("Arduino Rocks!");
     timerX = millis();
     Serial.println("Client Message Sent");
   }
 }
}


Here is what I get back in the serial monitor:
Code: [Select]
Ethernet ChatServer Online

31000

Client Message Sent

ARDUINO ROCKS!

647


I have a simple tcplistener running on my computer that echoes back whatever it receives.  As you can see it works on the first attempt and is about to attempt again after the 647.  But, that is it, nothing happens after that.  If the connect failed it should keep spitting out 647 until it connects because that value is not reset until a connection is made.  So it seems the Arduino simply hangs at that point.

mellis

Hmm, there were a couple of changes in the library, and it would be good to isolate which one caused the problem.

Can you change the following line in hardware/libraries/Ethernet/Client.cpp (in the Arduino application directory) from:

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

to:

 socket(_sock, Sn_MR_TCP, _port, 0);

Then delete hardware/libraries/Ethernet/Client.o and try recompiling and re-uploading your sketch.  Do you see the same problem?

Digger450

I made the change, but it still hangs on the second connection.

kmm

> Can you change the following line in hardware/libraries/Ethernet /Client.cpp (in the Arduino application directory) from:
>
>  socket(_sock, Sn_MR_TCP, _port, 1024 + _srcport);
>
> to:
>
>  socket(_sock, Sn_MR_TCP, _port, 0);

Should it not be

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

that is as this modification to the Client.cpp is listed in the orginal message by a "Paul_", see the posting with the title

 "Ethernet Library Bug :: No Repeated Connections..."

on "page 2" on this forum?

I have not yet got around testing it myself though...

regards,



kmm

Hi,

Tested it with the code above, but modified to use the standard "echo" service on port 7. Got a few TCP connection listed as open before the code somehow went into a "loop" writing the number "31075" to the serial console...

inetutils 15945        root    3u  IPv6  55938       TCP lupus.local:echo->10.0.0.177:1030 (ESTABLISHED)
inetutils 15946        root    3u  IPv6  55939       TCP lupus.local:echo->10.0.0.177:1031 (ESTABLISHED)
inetutils 16067        root    3u  IPv6  56394       TCP lupus.local:echo->10.0.0.177:1027 (ESTABLISHED)
inetutils 16068        root    3u  IPv6  56395       TCP lupus.local:echo->10.0.0.177:1028 (ESTABLISHED)
inetutils 16069        root    3u  IPv6  56396       TCP lupus.local:echo->10.0.0.177:1029 (ESTABLISHED)
inetutils 16072        root    3u  IPv6  56529       TCP lupus.local:echo->10.0.0.177:1025 (ESTABLISHED)
inetutils 16074        root    3u  IPv6  56567       TCP lupus.local:echo->10.0.0.177:1026 (ESTABLISHED)

regards,

mellis

kmm: I think you're right.  Does changing that line make it work better than with the default Arduino 0013 library?

kmm

Hi,

Tested it with the original Client.cpp file and it do block, as originally reported by MFV.

Thinking of it it is not so strange as it with the line

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

will create a new 5-tuple for the IP connection that is identical to the previous one. And that will not work. Changing the line into

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

will on the other hand create a new unique 5-tuple (the originating port number changes) for the IP connection like it should do.

regards,

Digger450

#9
Feb 10, 2009, 07:37 am Last Edit: Feb 10, 2009, 08:35 am by Digger450 Reason: 1
Hi,

I tested kmm's fix and it seemed to do the trick.  But I was still having a problem my TCPListener program not receiving any subsequent messages because it still thought the first connection was open.  I was able to get around this by adding a client.stop() after sending a message.  Looking at it now it's probably better to use the client.connected() to see if you are still connected rather than creating a new connection each time.  It may still be a good idea to add:

Code: [Select]
 if (connected())
   stop();


or maybe:

Code: [Select]
 if (connected())
   return 1;


to the begining of uint8_t Client::connect() in client.cpp?  What do you guys think?

I went ahead and modified the library to be compatible with Sanguino.  It tested fine with the modifications on both Arduino and Sanguino.

Good catch kmm!

Thanks guys.

MFV

Hello again,

ich changed
 socket(_sock, Sn_MR_TCP, _port, 1024 + _srcport);
into
 socket(_sock, Sn_MR_TCP, 1024 + _srcport, 0);
in hardware/libraries/Ethernet /Client.cpp

recompile my sketch but Arduino still send one request and stop then. Hm...

halley

#11
Feb 10, 2009, 05:00 pm Last Edit: Feb 10, 2009, 09:30 pm by halley Reason: 1
Whenever changing something in a library, remove all the *.o files in that library's folder.  Then recompile (verify) your sketch.

MFV


Dylan Bennett

Unfortunately, I spent an hour and a half up against this bug last night trying to figure out what I was doing wrong in my code. It didn't even occur to me that it might be a bug in the library. I figured it had to be my code, because, well, I'm new to this.

Glad to know it wasn't my code. An extra hour and a half of sleep would be nice, though.... :)

A.Selby

I made those changes but I think I have another problem, after 5 connections (one after another ... not at the same time) ... the arduino refuses any connection for about a minute ? Is there something I should be calling to free up those connections, besides client.stop(); ?

Go Up