I'm trying to get my Ethernet shield to connect to my web server. I have tested using the shield as its own server, and that works perfectly fine. I have also verified (using telnet) that the main server is working properly, and it is. The Arduino can't seem to connect to the server, however (or any server for that matter). My code is as follows:
#include <Dhcp.h>
#include <Dns.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>
#define PIN_TRIG 41
#define PIN_ECHO 39
byte MAC[] = { 0x13, 0xD9, 0xB8, 0x76, 0xE3, 0x68 };
IPAddress IP(140, 192, 242, 253);
IPAddress DNS(8, 8, 8, 8);
IPAddress GATEWAY(140, 192, 242, 1);
byte SERVER[] = { 65, 60, 27, 27 };
EthernetClient client;
void setup()
{
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
Serial.begin(9600);
Serial.println("Initializing Ethernet shield...");
Ethernet.begin(MAC, IP, DNS, GATEWAY);
delay(1000);
Serial.println("Ethernet shield initialized.");
}
float getRange()
{
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
int duration = pulseIn(PIN_ECHO, HIGH);
float inches = ((float) duration) / 148.0f;
return inches;
}
void loop()
{
int status = client.connect(SERVER, 2048);
if (status == 1)
{
Serial.println("Connected.");
Serial.println("Sending hello to server...");
client.println("TEST:HELLO");
client.stop();
}
else
{
Serial.print("Could not connect to server: error ");
Serial.println(status);
}
delay(2500);
}
It prints "Could not connect to server: error 0" to the serial console every 2.5 seconds. I modified EthernetClient.cpp to change its return values to figure out where the 0 was coming from, and I've narrowed it down to EthernetClient.cpp:66:
62 while (status() != SnSR::ESTABLISHED) {
63 delay(1);
64 if (status() == SnSR::CLOSED) {
65 _sock = MAX_SOCK_NUM;
66 return 0;
67 }
68 }
It appears as though the Arduino-side socket is immediately disconnecting directly after the connection attempt. Again, I've verified that a telnet client can connect and remain connected to the server, and communication works both directions, so it's not a server issue. Anybody have a clue as to why the shield is having such a hard time connecting?