I wrote a socket test sketch a few months ago. It is a web server, plus does a dns request for Google and ntp request every 30 seconds. It shows the socket status at different points during each. It shows for each socket
Socket#: D:(remote port)
The common status byte codes are
0x00 = available (not used)
0x14 = server waiting for client
0x17 = server with client
0x22 = udp
Insure network settings are correct, including a valid dns server.
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <utility/w5100.h>
#include <Dns.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,2);
IPAddress gateway(192,168,2, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dnServer(192,168,2,1);
IPAddress timeServer;
EthernetServer server(80);
EthernetUDP Udp;
DNSClient dnsC;
long timer = 0L;
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
void setup() {
Serial.begin(9600);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
Ethernet.begin(mac, ip, dnServer, gateway, subnet);
Serial.println("Start");
server.begin();
ShowSockStatus();
timer = millis();
dnsC.begin(dnServer);
if(dnsC.getHostByName("pool.ntp.org",timeServer)) {
Serial.print("\r\nNTP address = ");
Serial.println(timeServer);
}
else Serial.println("dns fail");
ShowSockStatus();
}
void loop()
{
IPAddress remoteAddr;
//Check if a web client has attached.
checkServer();
if ((millis() - timer) > 30000) {
if(dnsC.getHostByName("www.google.com",remoteAddr)) {
Serial.print(F("\r\nIP address = "));
Serial.println(remoteAddr);
}
else Serial.println(F("dns fail"));
ShowSockStatus();
delay(10);
timer = millis();
checkTime();
}
}
void ShowSockStatus()
{
for (int i = 0; i < MAX_SOCK_NUM; i++) {
Serial.print(F("Socket#"));
Serial.print(i);
uint8_t s = W5100.readSnSR(i);
Serial.print(F(":0x"));
Serial.print(s,16);
Serial.print(F(" "));
Serial.print(W5100.readSnPORT(i));
Serial.print(F(" 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(F("("));
Serial.print(W5100.readSnDPORT(i));
Serial.println(F(")"));
}
}
unsigned long sendNTPpacket(EthernetUDP & Udp, IPAddress& address)
{
memset(packetBuffer, 0, NTP_PACKET_SIZE);
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer,NTP_PACKET_SIZE);
Udp.endPacket();
}
void checkTime()
{
Udp.begin(8888);
sendNTPpacket(Udp, timeServer); // send an NTP packet to a time server
Serial.println("\r\nTime check");
ShowSockStatus();
// wait to see if a reply is available
delay(1000);
if ( Udp.parsePacket() ) {
// We've received a packet, read the data from it
Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer
//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
// now convert NTP time into everyday time:
Serial.print("Unix time = ");
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears;
// print Unix time:
Serial.println(epoch);
}
else Serial.println("No NTP packet received");
Udp.stop();
ShowSockStatus();
}
void checkServer()
{
EthernetClient client = server.available();
if(client) {
boolean currentLineIsBlank = true;
boolean currentLineIsGet = true;
int tCount = 0;
char tBuf[64];
int r,t;
char *pch;
Serial.println(F("\r\nServer client"));
ShowSockStatus();
Serial.print(F("\r\nClient request: "));
// this controls the timeout
int loopCount = 0;
while (client.connected()) {
while(client.available()) {
// if packet, reset loopCount
loopCount = 0;
char c = client.read();
if(currentLineIsGet && tCount < 63)
{
tBuf[tCount] = c;
tCount++;
tBuf[tCount] = 0;
}
if (c == '\n' && currentLineIsBlank) {
// send a standard http response
Serial.println(tBuf);
Serial.print(F("POST data: "));
while(client.available()) Serial.write(client.read());
Serial.println();
pch = strtok(tBuf,"?");
while(pch != NULL)
{
if(strncmp(pch,"t=",2) == 0)
{
t = atoi(pch+2);
Serial.print(F("t="));
Serial.println(t,DEC);
}
if(strncmp(pch,"r=",2) == 0)
{
r = atoi(pch+2);
Serial.print(F("r="));
Serial.println(r,DEC);
}
pch = strtok(NULL,"& ");
}
Serial.println(F("Sending response"));
client.print(F("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html>"));
client.println(F("<body><H1>TEST</H1>"));
client.println(F("<form method=GET>T: <input type=text name=t>
"));
client.println(F("R: <input type=text name=r>
<input type=submit></form>"));
client.println(F("</body></html>"));
client.stop();
}
else if (c == '\n') {
currentLineIsBlank = true;
currentLineIsGet = false;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
loopCount++;
// if 10000ms has passed since last packet
if(loopCount > 10000) {
// close connection
client.stop();
Serial.println(F("\r\nTimeout"));
}
// delay 1ms for timeout timing
delay(1);
}
Serial.println(F("done"));
}
}
edit: The timeServer ip will be assigned by dns from pool.ntp.org.
The available (0x00) and server waiting (0x14) sockets will show the remote ip and ports settings from the previous use of that socket.