Webclient, why is no one closing the connection?

hello forum!

could someone explain why no one (at least in the example code and other code i saw in the forum) is closing the connection via client.stop?

in this example:

if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {

the httpRequest function will not be executed until the connection is closed (and the interval…)
so i have to wait until the TCP/IP? connection will be closed from the webserver i am connecting to, am i getting this right?

in my testing this can be a good chunk of time 10-30seconds?

why not simply closing the connection after sending the HTTP Request?

    // send the HTTP PUT request:
    client.println("GET /latest.txt HTTP/1.1");
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");

is this connected to reading the output?

or why not reuse the connection if it is still running?

i am sure i am missing something basic here but please tell me what it is.

Thx for your time and help

Simple client test code.

//zoomkat 4-04-12
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 
//the arduino lan IP address { 192, 168, 1, 102 } may need 
//to be modified modified to work with your router.

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan assigned to arduino
//byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = { 208, 104, 2, 86 }; // zoomkat web page server IP address
EthernetClient client;

void setup(){

  Ethernet.begin(mac, ip);
  //Ethernet.begin(mac, ip, gateway, gateway, subnet);
  Serial.println("Better client test 4/04/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
      sendGET(); // call sendGET function below when byte is an e


void sendGET() //client function to send/receive GET request data.
  if (client.connect(myserver, 80)) {  //starts client connection, checks for connection
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println(); //end of get request
  else {
    Serial.println("connection failed"); //error message if no client connect

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 

  client.stop(); //stop client


well thank you zoomkat for your code, i think i have seen it in other threads as well.

i see you are closing the connection with client.stop after sending and reading , but that does not explain why the examples does not do it or which approach is "best practice"

to be honest your code explains nothing, it is just another example.

thx for your time

"Best practice" is send the request to the server, wait for the response, read the response until the server closes the connection, and then you close the connection on your end. How you do that is up to you. Zoomkat gave you his example. There are others also, but the ones that work follow "best practice".

i am sure i am missing something basic here but please tell me what it is.

What you are missing is not posting the total code you are having issues with, and not explaining the server to which you are trying to connect.