W5100/Ethernet cannot use socket as UDP after using it as TCP

So I've dummied up a test case from some of the Arduino samples. All this does is create a TCP server on port 80, and every 10 seconds it'll poll an NTP server via UDP. Here's what will happen:

  • It'll start listening on port 80 (W5100 socket #0)
  • It'll do the NTP stuff from port 8888 on W5100 socket #1. (this works fine).
  • It'll continue to do the NTP stuff every 10 seconds indefinitely and successfully.
  • If you connect to the port 80 (e.g. browse there with a web server), it'll service the request on socket 0, and open a new listening port on socket 1, and then terminate socket 0.
  • The NTP stuff continues every 10 seconds, but this time it's on socket 0. It works..
  • Browse to port 80 again. This time it'll service on socket 1.
  • NTP never works again. Web server works just fine forever...

Here's the sketch.

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <utility/W5100.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 timeServer(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, INADDR_NONE, gateway, subnet);
	delay(500);

	Serial.println("Start!");
	server.begin();
	ShowSockStatus();
	timer = millis();
}

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 

// send an NTP request to the time server at the given address 
unsigned long sendNTPpacket(EthernetUDP & Udp, IPAddress& address)
{

	// set all bytes in the buffer to 0
	memset(packetBuffer, 0, NTP_PACKET_SIZE); 
	// Initialize values needed to form NTP request
	// (see URL above for details on the packets)
	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
	// 8 bytes of zero for Root Delay & Root Dispersion
	packetBuffer[12]  = 49; 
	packetBuffer[13]  = 0x4E;
	packetBuffer[14]  = 49;
	packetBuffer[15]  = 52;

	// all NTP fields have been given values, now
	// you can send a packet requesting a timestamp: 		   
	Udp.beginPacket(address, 123); //NTP requests are to port 123
	Udp.write(packetBuffer,NTP_PACKET_SIZE);
	Udp.endPacket(); 
}

void checkTime()
{
	EthernetUDP Udp;
	Udp.begin(8888);
	sendNTPpacket(Udp, timeServer); // send an NTP packet to a time server
	Serial.println("");
	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("UDP FAIL!");
	Udp.stop();

}

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) > 10000) {
		timer = millis();
		checkTime();
	}
}