webClient example problem

HI Friends
really having a bad time

i am running webClient example given in the arduino IDE which gets a response from google server. BUT I AM NOT able to get response .
Every time i am getting disconnected. i dont know why. my internet connection is fine .

please any one help

my internet connection is fine .

Explain what this means. The fact that your PC is able to connect to a router, get an IP address, and therefore connect to the internet does not mean that the Arduino will also be able to do that.

The router has to accept that the Arduino has some specific IP address, if it is not responsible for assigning it one. The router then needs to address traffic it receives for the Arduino's IP address to the Arduino. If it doesn't recognize the Arduino's IP address as valid, packets addressed to it end up in the bit bucket.

HI
thanks a lot for replying and trying to help .
i think that the word "disconnecting" is the creating a mess.

now WHAT i meant by the statement ''that my internet connection is fine'' was :
my router is properly accepting the ip of my ethernet shield which was verified when i provided an IP in - webServer EXAMPLE given in arduino IDE and i got a response of some random values in my serial monitor which ultimately showed that router is accepting that IP..

NOW WHAT I MEANT BY ''disconnecting'' was :
the webClient sketch given in arduino IDE has some lines which says

/*
Web client

This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.

Circuit:

  • Ethernet shield attached to pins 10, 11, 12, 13

created 18 Dec 2009
modified 9 Apr 2012
by David A. Mellis

*/

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

// 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 };
IPAddress server(173,194,33,104); // Google
// *****************i even included the statement IPAddress ip( , , , ); **************

// 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;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

// 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(;:wink:
;
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");

// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}

void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting."); //********** this was the "disconnecting" i was talking about ***********
client.stop();

// do nothing forevermore:
for(;:wink:
;
}
}

i was trying to run this code as it is but i couldn't get connected to the client which in this case is the Google's server. i don't know why !! and where lies the problem !! ??

now hope i made myself clear .if you dont get any of my written matter please notify me...... PLEEEEESSSSSSSSS REPLY
i really want to cross this hurdle. plesss

This is no longer a valid ip for www.google.com
IPAddress server(173,194,33,104); // Not Google
This is tho
IPAddress server(74,125,227,148); // Google

if you dont get any of my written matter please notify me......

Well, I don't understand what you expect the compiler to do with all those smiley faces in your code. Use the # icon when posting code to stop the forum software from doing that to your code.

I don't understand why one period at the end of a sentence is not sufficient.... It is for most poeple... Why not for you?????????????

Simple client code you can try that connects to a web site I have.

//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.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.
{
  if (client.connect(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    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
    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

}

zoomkat thanks for ur sketch but it doesn't work with me

it gives me that in serial monitor

Better client test 4/04/12
Send an e in serial monitor to test

what's the problem please ??

What happens when you enter an 'e' and the enter key in the serial monitor like it prompted you to do?

it gives me

Better client test 4/04/12
Send an e in serial monitor to test
connection failed

disconnecting.

can you see where is the problem ?

Are those network settings correct for your localnet? Is there another device on that network that you can check the ip/subnet/gateway assignment?

//the arduino lan IP address { 192, 168, 1, 102 } may need 
//to be modified modified to work with your router.

Was it necessary? Did you make the change?

Is that the address of your router? It's not the address of my router.

Updated client test code that minimizes network configuration in the code.

//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted

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

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

char serverName[] = "web.comporium.net"; // zoomkat's 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 test 9/22/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(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println("Host: web.comporium.net");
    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

}

Updated client test code that minimizes network configuration in the code.

If you don't specify a DNS server, which one gets used? How does the server name actually get mapped to an IP address if no DNS server is specified?

thanks zoomkat
but i am using ENC28J60 ethernet with arduino IDE 2.3 and this is the webClient example code

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 42, 42 }; // will be modified with arduino IP on my lan
byte server[] = { 192, 168, 42, 1 }; // will be modified to google IP address

Client client(server, 80);

void setup() {
Ethernet.begin(mac, ip);
Serial.begin(9600);

delay(1000);
Serial.println("connecting...");

if (client.connect()) {
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
}
else {
Serial.println("connection failed");
}
}

void loop() {
if (client.available()) {
char c = client.read();
Serial.print(c);
}

if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for( ;; )
;
}
}

didn't work too

PaulS:

Updated client test code that minimizes network configuration in the code.

If you don't specify a DNS server, which one gets used? How does the server name actually get mapped to an IP address if no DNS server is specified?

The ethernet library uses the router ip (gateway) for the dns server. My router also performs dns for localnet clients, so it works ok for me. If that router doesn't, there may be trouble.

Thsi will not work on the ENC28J60 last I checked. Only W5100.

PaulS:

Updated client test code that minimizes network configuration in the code.

If you don't specify a DNS server, which one gets used? How does the server name actually get mapped to an IP address if no DNS server is specified?

So what DNS server do you specify for use by your preferred web browser?

So what DNS server do you specify for use by your preferred web browser?

I don't. The network administrators take care of that.

I'm not picking on you. I asked a serious question. I don't know what the Arduino does if no DNS server is specified. So, I'm asking.

This is from Ethernet.cpp. Note how each constructs the next ip and calls the next begin() below it. The dns server ip is constructed the same as the gateway.

void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip)
{
  // Assume the DNS server will be the machine on the same network as the local IP
  // but with last octet being '1'
  IPAddress dns_server = local_ip;
  dns_server[3] = 1;
  begin(mac_address, local_ip, dns_server);
}

void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server)
{
  // Assume the gateway will be the machine on the same network as the local IP
  // but with last octet being '1'
  IPAddress gateway = local_ip;
  gateway[3] = 1;
  begin(mac_address, local_ip, dns_server, gateway);
}

void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
{
  IPAddress subnet(255, 255, 255, 0);
  begin(mac_address, local_ip, dns_server, gateway, subnet);
}

void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
{
  W5100.init();
  W5100.setMACAddress(mac);
  W5100.setIPAddress(local_ip._address);
  W5100.setGatewayIp(gateway._address);
  W5100.setSubnetMask(subnet._address);
  _dnsServerAddress = dns_server;
}

So, there are expectations built into the library as to what the gateway, DNS server, subnet mask, etc. are that are not explained in the documentation.

These expectations are not necessarily the same expectations built into the other library, and are most likely not valid for OPs situation.

PaulS:
So, there are expectations built into the library as to what the gateway, DNS server, subnet mask, etc. are that are not explained in the documentation.

These expectations are not necessarily the same expectations built into the other library, and are most likely not valid for OPs situation.

Yes and yes. But the second should be no. It should be so easy to change that one last call in the last begin function to a 28J60 setup call. It is all there for you, regardless of the processor you are about to initialize.