Here's a much simpler example using the built in DNS library:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <utility/w5100.h>
#include "Dns.h"
void ShowSockStatus()
{
for (int i = 0; i < MAX_SOCK_NUM; i++) {
Serial.print("Socket#");
Serial.print(i);
uint8_t s = W5100.readSnSR(i);
Serial.print(":0x");
Serial.print(s,16);
Serial.print(" ");
Serial.print(W5100.readSnPORT(i));
Serial.print(" D:");
uint8_t dip[4];
W5100.readSnDIPR(i, dip);
for (int j=0; j<4; j++) {
Serial.print(dip[j],10);
if (j<3) Serial.print(".");
}
Serial.print("(");
Serial.print(W5100.readSnDPORT(i));
Serial.println(")");
}
}
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server(80);
IPAddress dnsserver(192,168,10,1);
IPAddress ip(192,168,10,20);
IPAddress gateway(192,168,10, 2);
IPAddress subnet(255, 255, 255, 0);
long timer;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip, dnsserver, gateway, subnet);
delay(500);
Serial.println("Start!");
server.begin();
ShowSockStatus();
timer = millis();
}
void loop()
{
//Check if a web client has attached.
EthernetClient client = server.available();
if (client) {
Serial.println("new Client");
ShowSockStatus();
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
if (c == '\n' && currentLineIsBlank) {
client.stop();
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
if ((millis() - timer) > 5000) {
DNSClient dns;
IPAddress remote_addr;
dns.begin(dnsserver);
if (dns.getHostByName("www.yahoo.com", remote_addr) == 1)
{
for (int i=0; i<4; i++) {
Serial.print(remote_addr[i]);
if (i!=3)
Serial.print('.');
else
Serial.println("");
}
}
else
Serial.println("DNS FAIL!");
timer = millis();
ShowSockStatus();
}
}
And the output
Start!
Socket#0:0x14 80 D:0.0.0.0(0)
Socket#1:0x0 0 D:0.0.0.0(0)
Socket#2:0x0 0 D:0.0.0.0(0)
Socket#3:0x0 0 D:0.0.0.0(0)
98.139.183.24
Socket#0:0x14 80 D:0.0.0.0(0)
Socket#1:0x0 1030 D:192.168.10.1(53)
Socket#2:0x0 0 D:0.0.0.0(0)
Socket#3:0x0 0 D:0.0.0.0(0)
98.138.253.109
Socket#0:0x14 80 D:0.0.0.0(0)
Socket#1:0x0 1024 D:192.168.10.1(53)
Socket#2:0x0 0 D:0.0.0.0(0)
Socket#3:0x0 0 D:0.0.0.0(0)
98.138.253.109
Socket#0:0x14 80 D:0.0.0.0(0)
Socket#1:0x0 1024 D:192.168.10.1(53)
Socket#2:0x0 0 D:0.0.0.0(0)
Socket#3:0x0 0 D:0.0.0.0(0)
new Client
Socket#0:0x17 80 D:192.168.10.213(11298)
Socket#1:0x17 80 D:192.168.10.213(11299)
Socket#2:0x14 80 D:0.0.0.0(0)
Socket#3:0x0 0 D:0.0.0.0(0)
GET / HTTP/1.1
Host: 192.168.10.20
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
client disconnected
98.139.183.24
Socket#0:0x0 1035 D:192.168.10.1(53)
Socket#1:0x17 80 D:192.168.10.213(11299)
Socket#2:0x14 80 D:0.0.0.0(0)
Socket#3:0x0 0 D:0.0.0.0(0)
98.138.253.109
Socket#0:0x0 1029 D:192.168.10.1(53)
Socket#1:0x17 80 D:192.168.10.213(11299)
Socket#2:0x14 80 D:0.0.0.0(0)
Socket#3:0x0 0 D:0.0.0.0(0)
98.139.183.24
Socket#0:0x0 1039 D:192.168.10.1(53)
Socket#1:0x0 80 D:192.168.10.213(11299)
Socket#2:0x14 80 D:0.0.0.0(0)
Socket#3:0x0 0 D:0.0.0.0(0)
new Client
Socket#0:0x17 80 D:192.168.10.213(11312)
Socket#1:0x14 80 D:192.168.10.213(11299)
Socket#2:0x17 80 D:192.168.10.213(11311)
Socket#3:0x0 0 D:0.0.0.0(0)
GET / HTTP/1.1
Host: 192.168.10.20
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
client disconnected
98.138.253.109
Socket#0:0x17 80 D:192.168.10.213(11312)
Socket#1:0x14 80 D:192.168.10.213(11299)
Socket#2:0x0 1033 D:192.168.10.1(53)
Socket#3:0x0 0 D:0.0.0.0(0)
98.139.183.24
Socket#0:0x17 80 D:192.168.10.213(11312)
Socket#1:0x14 80 D:192.168.10.213(11299)
Socket#2:0x0 1028 D:192.168.10.1(53)
Socket#3:0x0 0 D:0.0.0.0(0)
98.138.253.109
Socket#0:0x17 80 D:192.168.10.213(11312)
Socket#1:0x14 80 D:192.168.10.213(11299)
Socket#2:0x0 1039 D:192.168.10.1(53)
Socket#3:0x0 0 D:0.0.0.0(0)
DNS FAIL!
Socket#0:0x0 1034 D:192.168.10.1(53)
Socket#1:0x14 80 D:192.168.10.213(11299)
Socket#2:0x0 1039 D:192.168.10.1(53)
Socket#3:0x0 0 D:0.0.0.0(0)
DNS FAIL!
Socket#0:0x0 1033 D:192.168.10.1(53)
Socket#1:0x14 80 D:192.168.10.213(11299)
Socket#2:0x0 1039 D:192.168.10.1(53)
Socket#3:0x0 0 D:0.0.0.0(0)
Notice how it works right up till it tries to reuse socket #0 to do the DNS (which has previously been used as UDP/DNS and then TCP). Then it fails...