8 Replies Latest reply on Aug 4, 2014 6:57 PM by Rushang.Karia

    HTTP Connection: close -> if (!client.connected()) - Galileo

    gilco

      Hi,

       

      I am sending a HTTP with "Connection: close" request:

       

      client.println("GET http://www.google.com.au/ HTTP/1.1");          // note, the google address is not the real one

      client.println("Connection: close");

      client.println();

       

      I get a reply that the server acknowledge it:

       

      HTTP/1.1 200 OK

      Date: Mon, 04 Aug 2014 05:57:35 GMT

      Expires: -1

      Cache-Control: private, max-age=0

      Content-Type: text/html; charset=ISO-8859-1

      Set-Cookie: PREF=ID=c7372cf97f6b7a33:FF=0:TM=1407131855:LM=1407131855:S=ivf0-SM_bxoycH8J; expires=Wed, 03-Aug-2016 05:57:35 GMT; path=/; domain=.google.com.au

      Set-Cookie: NID=67=Khn-N2-2ORq6_9hmPCkpXKWskUWbNEXE3UNsX87sveVkOsBfGwEDPU-Y50NmRfKV9JWy-jf5q_sgwGUf36UivdQY_gNUiFaETyExB69pqCt3VXDiZ_7oO1KUHnqx_c2R; expires=Tue, 03-Feb-2015 05:57:35 GMT; path=/; domain=.google.com.au; HttpOnly

      P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."

      Server: gws

      X-XSS-Protection: 1; mode=block

      X-Frame-Options: SAMEORIGIN

      Alternate-Protocol: 80:quic

      Connection: close

       

      But the code that need to pick it up, doesn't and the stay in endless loop:

       

      void loop() {

        if (client.available()) {

          char c = client.read();

          Serial.print(c);

        }

       

         if (!client.connected()) {

            Serial.println("disconnecting");

            client.stop();

            delay(2000);

            while(true);

          }

        }

       

      Does the Ardiuno have a problem to close the connection?

        • 1. Re: HTTP Connection: close -> if (!client.connected()) - Galileo
          Intel_Alvarado

          Hi gilco,

           

          In order to provide a better response for your question, may I have more information on what you are trying to do? Can you post all of your steps exactly as you did them, as well as the complete sketch?

           

          Regards,

          Sergio

          • 2. Re: HTTP Connection: close -> if (!client.connected()) - Galileo
            Rushang.Karia

            I am not sure what you meant but on looking at your code I saw something.


               if (!client.connected()) {

                  Serial.println("disconnecting");

                  client.stop();

                  delay(2000);

                  while(true);

                }

            The last line (while (true)) will run forever and never exit if it is reached. Perhaps if you remove it you might get the desired functionality.

            • 3. Re: HTTP Connection: close -> if (!client.connected()) - Galileo
              gilco

              Hi,

               

              Thank you for the respond. I am just trying to follow a simple demo from the Arduino Site:

              Arduino - WebClient

               

              The "while (true);"loop suppose to stop the program after it did a basic HTTP read from a site.

              Then, according to the Arduino site, it suppose to recongnize it with the command if (!client.connected()) and stop.

              It doesn't.. (ether with the original code)

               

              I hope I am more clear now.

               

              #include <SPI.h>

              #include <Dhcp.h>

              #include <Dns.h>

              #include <Ethernet.h>

              #include <EthernetClient.h>

              #include <EthernetServer.h>

              #include <EthernetUdp.h>

               

              byte mac[] = {  0x98, 0x4F, 0xEE, 0x01, 0x46, 0xCF };

              EthernetClient client;

              int i = 0;

              char site[] = "www.bananafilm.com";

              String buffer;

              String[] split(String message);

               

              void setup() {

               

                // initialize network so DHCP can start working

                system("/etc/init.d/networking restart");

                delay(2500);

                // start the serial library:

                Serial.begin(9600);

                Serial.println("Testing 1 2 3 .... hello world");

                Serial.flush();

                // start the Ethernet connection:

                if (Ethernet.begin(mac) == 0) {

                  Serial.println("Failed to configure Ethernet using DHCP");

                  // no point in carrying on, so do nothing forevermore:

                  for(;;)

                    ;

                }

                // print your local IP address:

                Serial.println(Ethernet.localIP());

               

                // send to port 80 of the following site address a HTTP request.

                if (client.connect(site, 80)) {

                  Serial.println("connected");

                  // Make a HTTP request:

                  client.println("GET /cloud/?device=Galileo HTTP/1.1");

                  client.println("Host: www.bananafilm.com");

                  client.println("Connection: close");

                  client.println();

                }

                else {

                  // kf you didn't get a connection to the server:

                  Serial.println("connection failed");

                }

                Serial.println("Message Sent");

              }

               

               

              void loop() {

                // record message to String

                if (client.available()) {

                  char c = client.read();

                  buffer.concat(c);

                  Serial.print(c);

                }

               

                  if (!client.connected()) {

                    Serial.println("disconnecting");

                    client.stop();

                    while(true);    // stop loop for ever

                }

              }

              • 4. Re: HTTP Connection: close -> if (!client.connected()) - Galileo
                Rushang.Karia

                Hi, Thanks for the example. Things are more clearer now.

                 

                Can you try this

                 

                if(!client.available()) instead of if(!client.connected())

                {

                //all other code here

                }

                 

                client.connected() returns false if the connection to the server got disconnected. In your case, the server never disconnects itself so the client is always connected.

                However, client.available() returns true only when there are incomning bytes from the server. This means the webpage is not done. So at some point client.avaliable with return false and that should be your cue to stop the client.

                 

                Hopefully this helps

                • 5. Re: HTTP Connection: close -> if (!client.connected()) - Galileo
                  gilco

                  Hi,

                   

                  Yup, that was understanding as well; but correct me if I am wrong, when we send an HTTP command for connection close, does it means the Server suppose to end the connection after it rely to Client?

                   

                  Gil

                  • 6. Re: HTTP Connection: close -> if (!client.connected()) - Galileo
                    Rushang.Karia

                    When you send a connection close request it instructs the Server to release all resources pertaining to that client. The connection ends from the Server side.

                    I think it depends upon the protocol when the connection is closed.

                     

                    For example in TCP there is a thing called three way handshaking, in which client request to close, Server replies okay and sends whatever last data is to be sent. The client then sends a final acknowledgement.

                     

                    In this case the server actually waits for the last ACK before closing the connection.

                     

                    I dont know what it is for HTTP and I am not sure if this answers your question. Let me know

                    • 7. Re: HTTP Connection: close -> if (!client.connected()) - Galileo
                      gilco

                      yup, you are correct, the connection need to be closed from Client side... I guess they have a bug in the code at the Arduino site..

                       

                      Thank you for your help.

                       

                      Gil :-)

                      • 8. Re: HTTP Connection: close -> if (!client.connected()) - Galileo
                        Rushang.Karia

                        Welcome.

                         

                        Do not hesitate to ask questions on the forum.