Go Down

Topic: The ClientConstructor Example Does Not Seem To Compile (Read 936 times) previous topic - next topic

12970

Hi Everyone:

I have a Mega with an Ethernet Shield running in "Client" mode using a static IP to address a "server".  (Version 1.0.1)
The code runs very well, except I would like to address the server by name.

As I began looking into this, I decided to cut and paste this code to play around with it.

http://arduino.cc/en/Reference/ClientConstructor

I can NOT figure out what I am doing wrong; but, this code just will not compile for me.
When I look at the .h files, I agree with the compiler errors.
I tried running this under 1.0.1 and 1.0... with essentially the same results.

Any thoughts would be greatly appreciated. 

Thanks

-Rick


johnwasser

The first error I got with that code was "Client is now called EthernetClient".  Perhaps using the example for EthernetClient would work better:

http://arduino.cc/en/Reference/EthernetClient

It looks very similar to the Client code except (server, 80) moved from the Client constructor to the client.connect() call.  This allows a single Client instance to connect to different servers at different times.

Code: [Select]

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte server[] = { 64, 233, 187, 99 }; // Google

EthernetClient client;

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

  delay(1000);

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

  if (client.connect(server, 80)) {
    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(;;)
      ;
  }
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

12970

Hi John:

This makes sense...  I missed the first error message.

Thanks for taking a look.

-Rick

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy