Client.connected() always returns false

The main problem, as far as I realise, is that the check if(Client.connected()) returns 0 and I cannot discover the cause. I use different libraries (not the usuall libraries) because the command Ethernet.begin(mac); would fail and nothing else would run after this command.

The code is a known program and is given:

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

#include <Dhcp.h>
#include <Dns.h>
#include <ethernet_comp.h>
#include <UIPClient.h>
#include <UIPEthernet.h>
#include <UIPServer.h>
#include <UIPUdp.h>



/*
 Simple Web Data Client

 This sketch connects to a website using an Arduino Wiznet Ethernet shield,
 downloads a simple dataset, and does some LED fades based on the data.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13

 created december 2014
 by Nathan Yau, based on work by David A. Mellis, Tom Igoe, and Adrian McEwen

 This example code is in the public domain.
 */



// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
char server[] = "projects.flowingdata.com";
String dataLocation = "/holidays/current-rates.txt HTTP/1.1";

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
String currentLine = "";            // string for incoming serial data
String currRates = "";
boolean readingRates = false;       // is reading?
const int requestInterval = 600000; // milliseconds delay between requests

boolean requested;                  // whether you've made a request since connecting
long lastAttemptTime = 0;           // last time you connected to the server, in milliseconds
IPAddress ip(11,164,2,156);

// LED Stuff //

// Variables for first LED
// int led1 = 3;                       // the pin that the LED is attached to
// int brightness1 = 0;                // how bright the LED is
// int maxBrightness1 = 100;           // how bright before turning fade
// int fadeAmount1 = 1;                // how many points to fade the LED by
// int delayTime1 = 40;
// float tweetsPerMin1 = 0;
// long lastFadeTime1 = 0;

// Variables for second LED
// int led2 = 5;
// int brightness2 = 0;
// int maxBrightness2 = 100;
// int fadeAmount2 = 1;
// int delayTime2 = 40;
// float tweetsPerMin2 = 0;
// long lastFadeTime2 = 0;

// Variables for third LED
// int led3 = 9;
// int brightness3 = 0;
// int maxBrightness3 = 100;
// int fadeAmount3 = 1;
// int delayTime3 = 40;
// float tweetsPerMin3 = 0;
// long lastFadeTime3 = 0;

// Data baselines
float baselineRate = 500;          // Tweets per minute
int baselineDelay = 10;             // Milliseconds between fade increments
int minBrightness = 10;             // Minimum top brightness when fading


void setup() {

  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  // start the Ethernet connection:
  Ethernet.begin(mac);
Serial.println(Ethernet.localIP());
  // Connect to server
  connectToServer();

  // LEDs
  // pinMode(led1, OUTPUT);
  // pinMode(led2, OUTPUT);
  // pinMode(led3, OUTPUT);

}


void loop() {

  // Set LED fade rates and brightness
  // analogWrite(led1, brightness1);
  // analogWrite(led2, brightness2);
  // analogWrite(led3, brightness3);

  // Fade accordingly for next iteration
  // if (millis() - lastFadeTime1 > delayTime1) {
  //   fadeByRate(tweetsPerMin1, brightness1, fadeAmount1, delayTime1, maxBrightness1, lastFadeTime1);
  // }
  // if (millis() - lastFadeTime2 > delayTime2) {
  //   fadeByRate(tweetsPerMin2, brightness2, fadeAmount2, delayTime2, maxBrightness2, lastFadeTime2);
  // }
  // if (millis() - lastFadeTime3 > delayTime3) {
  //   fadeByRate(tweetsPerMin3, brightness3, fadeAmount3, delayTime3, maxBrightness3, lastFadeTime3);
  // }

  // Checking for new data
  if (client.connected()) {

    pinMode(4,OUTPUT);
    digitalWrite(4,HIGH);
    if (client.available()) { //[color=red]PROBLEM!!![/color]
      pinMode(4,OUTPUT);
      digitalWrite(4,HIGH);
      // read incoming bytes:
      char inChar = client.read();

      // add incoming byte to end of line:
      currentLine += inChar;

      // if you get a newline, clear the line:
      if (inChar == '\n') {
        currentLine = "";
      }

      if (currentLine.endsWith("<rates>")) {
        readingRates = true;
      }
      else if (readingRates) {
        if (!currentLine.endsWith("</rates>")) { //'>' is our ending character
          currRates += inChar;
        }
        else {
          readingRates = false;

          String justRates = currRates.substring(0, currRates.length() - 8);
          Serial.println(justRates);

          // Split justRates
          int firstSpaceIndex = justRates.indexOf(" ");
          int secondSpaceIndex = justRates.indexOf(" ", firstSpaceIndex + 1);
          String firstVal = justRates.substring(0, firstSpaceIndex);
          String secondVal = justRates.substring(firstSpaceIndex + 1, secondSpaceIndex);
          String thirdVal = justRates.substring(secondSpaceIndex);

          // Convert strings to floats and store
          int tweetsPerMin1 = strToFloat(firstVal);
          int tweetsPerMin2 = strToFloat(secondVal);
          int tweetsPerMin3 = strToFloat(thirdVal);

          // Reset for next reading
          currRates = "";
          
        }
      }
    }
    client.stop();
  }
  else if (millis() - lastAttemptTime > requestInterval) {
    // if you're not connected, and two minutes have passed since
    // your last connection, then attempt to connect again:
    connectToServer();
  }
}


void connectToServer() {
  // attempt to connect, and wait a millisecond:
  Serial.println("connecting to server...");
  
client.connect(server, 80); // Dummy call
client.stop();
delay(1000);
  if (client.connect(server, 80)) {
    Serial.println("making HTTP request...");

    // make HTTP GET request to dataLocation:
    client.println("GET " + dataLocation);delay(1000);
    Serial.println("GET " + dataLocation);
    client.println("Host: projects.flowingdata.com");delay(1000);
    Serial.println("Host: projects.flowingdata.com");
    client.println();delay(5000);
    
  }
  // note the time of this connect attempt:
  lastAttemptTime = millis();
}


// Calculate brightness and maxBrightness based on rate.
void fadeByRate(float& tweetsPerMin, int& brightness, int& fadeAmount, int& delayTime, int& maxBrightness, long& lastFadeTime) {

  // maxBrightness between 80 and 255
  maxBrightness = min( max(round(tweetsPerMin / baselineRate * 255), minBrightness), 255 );

  // Blink lightly if low rate
  if (maxBrightness == minBrightness) {
    if (brightness == 0) {
      brightness = minBrightness;
      delayTime = round(5000 * tweetsPerMin / 100);
    }
    else {
      brightness = 0;
      delayTime = 5000;
    }
  }

  // Fade otherwise
  else {
    delayTime = 40;

    // Reverse direction when faded out or at maxBrightness
    if (brightness == 0) {
      fadeAmount = abs(fadeAmount);
    }
    else if (brightness >= maxBrightness) {
      fadeAmount = -abs(fadeAmount);
      delayTime = round(10000 * tweetsPerMin / baselineRate);
      Serial.println(delayTime);
    }

    // Adjust brightness.
    brightness += fadeAmount;
  }

  lastFadeTime = millis();
}


float strToFloat(String str) {
  char carray[str.length() + 1];           // determine size of array
  str.toCharArray(carray, sizeof(carray)); // put str into an array
  return atof(carray);
}

Any help or indication would be appreciated

const int requestInterval = 600000; // milliseconds delay between requests

const or not, 600000 is NOT a valid int value.

I use different libraries (not the usuall libraries) because the command Ethernet.begin(mac); would fail and nothing else would run after this command.

So, you can't initialize a connection between the Arduino and the ethernet shield, so you figure that it's OK to not do that, and you'll still be able to talk to the ethernet shield. I must admit that I don't understand that logic.

You are not evaluating the return value for client.connect properly. That function will return true if the server is a domain name and the dns resolution fails. The only return value that indicates a successful connection is 1.

// change this
  if (client.connect(server, 80)) {
// to this
  if (client.connect(server, 80) == 1) {

what if the value always true? i have tried
if (client.connect(server, 80) == 1) {
but its not working..
or is there any other way to check weather the server is correct or not?

or is there any other way to check weather the server is correct or not?

That IS the way. If it says not connected, it means NOT CONNECTED!

Why do you persist in believing that the Arduino is wrong?

sorry, i dont think that the arduino is wrong, and i dont say its not connected. for my case is its always connected. may i ask something else?

when i set the server is "192.168.0.45" the value is true. if i set the server 192.168.0.45/123.php , but there is no 123.php in that server, will the value be true too?

when i set the server is "192.168.0.45" the value is true. if i set the server 192.168.0.45/123.php , but there is no 123.php in that server, will the value be true too?

Why don't you ask your Arduino?

192.168.0.45/123.php is NOT a server address.

Simple client test code you can try unmodified to see if an outside server can be reached.

//zoomkat 3-1-13
//simple client checkip test
//for use with IDE 1.0.1 or later
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "checkip.dyndns.com"; // test web page server
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client ip test 3/1/13"); // 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(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET / HTTP/1.1"); //download text
    client.println("Host: checkip.dyndns.com");
    client.println("Connection: close");  //close 1.1 persistent connection  
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  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 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}