Understanding ethernet connections

I'm using Arduino's Ethernet shield R3 with the twitter.h library and EthernetUdp.h library. My Arduino will monitor for a water leak. If detected it will check the time using NTP Timer server. I'm basing the time look-up on this code. It uses UDP and port 8888.

I don't know much about how the Ethernet connections work. So far my experience with Ethernet and the Arduino is that the connections are not that reliable. My experience is mostly uploading to COSM.com and to a lesser degree open.sen.se. Typically the uploads to COSM and open.sen.se stop working after a while (less then a day) on both services. I've found the solution is to just automatically reboot my Arduino every hour.

With this leak detection project sending tweets out, I will only be connecting to the internet very infrequently. When a leak is detected I'll get the time from the NTP time server, add this the time to my twitter message and send out the tweet. I did some testing and these two things seem to work fine together on my Arduino. Right now I use this in setup() to initialize the Ethernet and UDP.

  if (Ethernet.begin(mac) == 0) 
  {  // Takes about 60 seconds to fail
    Serial.println("Failed to configure Ethernet using DHCP");
    Serial.println("Connected to Ethernet using DHCP");

I know the Ethernet library uses 4 connections, so would this setup use two, one for twitter and one for UDP? Since I'm rarely going to send a tweet out, should I use Ethernet.begin() only when I'm going to send a tweet out instead of in setup(). If I did do that, I guess I would need a way to shutdown the connection so I didn't run Ethernet.begin() again. Would something like that be a better way to handle my Ethernet connection?


I know the Ethernet library uses 4 connections, so would this setup use two, one for twitter and one for UDP?

Yes. I use two connections simultaneously, but you must be careful about opening and closing the connections. If not done right, it will lock up after running a while. The normal cause for the lockup will be no available socket on the ethernet shield because all 4 sockets are still open.

edit: I use two connections only with client code, not server code. The server can end up using all the connections for inbound requests, and there will be no socket available for outbound requests.