Pages: [1]   Go Down
Author Topic: The ClientConstructor Example Does Not Seem To Compile  (Read 814 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8669
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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(;;)
      ;
  }
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi John:

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

Thanks for taking a look.

-Rick
Logged

Pages: [1]   Go Up
Jump to: