Arduino webclient example fails to connect

Hi, i have working on this problems mindlessly since hours. The problem is that the webclient example won't work at all, it wouldn't go in the condition of client.connect(server,80), i have looked at all the forums on the website but couldn't get the arduino to function properly as a webclient. I looked at the solution provided by the user ZOOMKAT on various forums but couldn't get it to function. I am confused as the webserver example works fine. I tried different combination of ip addresses but none worked. I also tried changing firewall settings for port 80 to allow communication through the port. Still i couldn't reach any solution.

Here is code:

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x28, 0x67 }; //physical mac address
byte ip[] = { 169, 254, 199, 142 }; // 
//ip in lan assigned to arduino .the same ip used the webserver example 

byte myserver[] = { 173,194,40,159 }; 
//ip address of www.google.com.pk

EthernetClient client;
//////////////////////

void setup(){

  Ethernet.begin(mac, ip);
//DHCP fails to assign an ip address that is why i am hardcoding the ip
  Serial.begin(9600); 
  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.
{
  Serial.println("im in send");
  delay(1000);
client.connect(myserver, 80);
  if (client.connect(myserver, 80)) {  //starts client connection, checks for connection
   Serial.println("Im in");
   client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com.pk");
    client.println("Connection: close");
    client.println();} 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1000); //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

}

//DHCP fails to assign an ip address that is why i am hardcoding the ip

Is the ethernet shield connected to a router or your PC?

There is no router that I know of that uses this localnet.

byte ip[] = { 169, 254, 199, 142 }; //

Ethernet Shield is Connected to PC.I made the webserver example work by connecting ethernet shield to the PC and assigning it an IP address. I got to know the IP address by cmd ,ipconfig command. I used the ip that was appearing on the ethernet adapter ipv4 line.

The web server example may work like that, but the client won't. It might be able to access a server installed on the PC. Otherwise it needs to be routed through the PC to access the internet. That is normally done with ICS (Internet Connection Sharing).

goldriver:
byte myserver[] = { 173,194,40,159 };
//ip address of www.google.com.pk

That's not true when accessing the website 'www.google.com.pk' from Germany.

I get:

Routenverfolgung zu www.google.com.pk [173.194.32.223]

The website seems to be operated by a "server pool" of different servers (secret agency activities in that country?) with different IP numbers.

So why do you not access the server by name?

jurs:
That's not true when accessing the website 'www.google.com.pk' from Germany.

I get:

Routenverfolgung zu www.google.com.pk [173.194.32.223]

The website seems to be operated by a "server pool" of different servers (secret agency activities in that country?) with different IP numbers.

That is called "load balancing". It distributes the load among several servers so it doesn't bog down one server. Many major internet companies use that. Any of the server IPs in that "pool" should work.

If the client is unable to connect to the internet, then probably any dns request the client attempts will also fail.

SurferTim:
If the client is unable to connect to the internet, then probably any dns request the client attempts will also fail.

OK, so here is the bullshit I found in the code:

  delay(1000);
client.connect(myserver, 80);
  if (client.connect(myserver, 80)) {  //starts client connection, checks for connection

This piece of code needs to be replaced with just the last line:

  if (client.connect(myserver, 80)) {  //starts client connection, checks for connection

i am aware. But that doesnt the solve problem. I put that line on purpose to test something.

i have tried using only the name google and not the ip address but that doesn't solve the problem.

let me re-iterate my problem again:

my ethernetshield+controller is connected to my laptop and i am trying to run the web client example. Would you recommend connecting it to the router? or is there any network or router setting i am supposed to do for this particular example?

goldriver:
my ethernetshield+controller is connected to my laptop and i am trying to run the web client example. Would you recommend connecting it to the router?

Wrong network configuration.

If you connect the Ethernetshield to a laptop computer, this laptop computer would need a complicated "bridge network" setup, or the network traffic will not be routed across your laptop computer into the Internet.

So, of course, your Ethernet shield has to be connected to your Internet router and not to a client computer within your network.

Thanks it works now!

I have one more question, fine,the client code works now and making GET requests properly. however, i am trying to access a php file in my www folder in WAMP. But this is what it results in :

403 Forbidden

Forbidden

You don't have permission to access /ardtest.php on this server.

I have tried changing wamp security settings but to no avail

edit: If you plan on using a domain name, insure you evaluate the return value correctly. The only success return value is 1. Any other non-zero return values are dns fails.

  if (client.connect(myserver, 80) == 1) {  //starts client connection, checks for connection