Wiznet repeated connections

Hi All, I have the following code in my main loop...

if (takeSampleNow)  // check flag at this point. occurs every second or 1000 overflows of the timer
  {
    TakeSample();
    if (client.connect()) {
      client.println("GET /~latenigh/test.php?currentspeed=25.1&directiontext=S&directiondegrees=180&units=MPH&status=1 HTTP/1.0");
      client.println();
      client.stop();
    } 
    else {
      Serial.println("connection failed");
    }
    takeSampleNow = 0; // turn flag off
  }

my question is this. it seems like connecting and stopping the connection once every second is causing a ton of traffic. Is there a way to open a connection at boot up and just make sure it is open and send my message once per second? as opposed to the open and close method. i couldn't get it to work the other way. it sent about 3 or 4 messages and then disconnected.

watch the wraps if any

thanks in advance Jeff

The short answer is... yes.

The long answer is... maybe.

A TCP connection will eventually time out if there is no activity. So the trick is to ensure that you keep the connection active.

That begs the question... "how long does it take for a TCP connection to time out?"

That's where 'maybe' comes in... because there is no standard for timeout.

==================================

As a rule of thumb, sending data every second will avoid almost all timeouts.

yes but as i said in my original post i tried opening the connection once and then sent my data every second and it failed on the 4th or 5th try every time. so now i connect and stop once every second but it just doesn't seem right and i'm wondering how reliable it will be in the long term. so what are other people doing who have to send data once per second?

I don't understand this:

it seems like connecting and stopping the connection once every second is causing a ton of traffic

...because connecting is pretty simple relative to sending and receiving data. You shouldn't have a 'ton of traffic' just connecting/disconnecting. Sending/receiving the data over the connection is where the real traffic is.

If you aren't referring to the overhead of the HTTP protocol's headers, then this may help - Since you are actually using the HTTP protocol in your posted code (I can't see the HTTP version, it is too wide for the code box), but If you are using Http/1.0, you are going to need to specify keep-alive headers to request that the server keep the connection alive. (Connection: Keep-Alive) In HTTP/1.1 - all connections are assumed keep-alive unless 'Connection: Close' is specified in the headers. However, remember that since it is a "request" to keep alive, the server software could at any time deny the request and close the connection anyway.

Alternatively, if you don't like the overhead of the HTTP headers, then probably you are going to want to make your own protocol over tcp/ip (like the chat server sample) where you send

[data] and the server responds with [result] and keeps the connection open.  

Regardless, you will still have to use a if(client.connected()){send()} else{reconnect() send()} somewhere in both protocols to see if the socket has been closed by the server at each loop.

In my code, I do a connect, Post/Get and then a close every time.  Mind you, I do this every time a sensor changes, and not every loop - but since August it has been very reliable and handles network outages with ease...

I'm using HTTP/1.0 I think there is a slider at the bottom of the code window that you can slide to see the rest of that line. I 'm new to this Web Server stuff and don't know what is the norm or reasonable for this situation. I guess basically i'm just worried about the hosting company complaining about the repeated connections every second and trying to shut me down. Also really i'm just trying to learn what other people do in this situation. Thanks.

I think there is a slider at the bottom of the code window that you can slide to see the rest of that line.

Thanks - I didn't see that while posting my reply.

The 1 connection per second shouldn't be something they even notice. I haven't seen any documents from any of my hosting providers indicating I have too many connections. They sure notice my bandwidth utilization and transfered byte count though!

Thanks!