Skip to content

Handling Multiple Sockets With the Wiznet W5500

August 21, 2015

When I did the simplest server with the W5500 I deliberately restricted myself to socket 0. I noticed though that the arduino ethernet library started with socket 0 and as soon as a session was established it began listening on socket 1. I tried simulating heavy traffic on a slow server(no trouble with that part) and I found that i could get a TCP error if the server had no sockets listening.

I want to open up the doughnuts server during the Midwest Vintage Computer Festival (VCFMW) later this month so figured i would try at least two sockets.

I changed the lowest level routines in w5500simplestcode.c to take a socket parameter (renaming them to for example wizGetCtl16X(whichsocket, SnRX_RSR)) and I’ve changed the main program to switch sockets for listening when a session is established.

void main(void){
	unsigned char socketXstatus;
	unsigned char whichsocket=0;//start with socket 0
	unsigned char gotsocket;//socket which just received data
    unsigned char ip_addr[] = {10,0,0,180};//{169,254,180,2};
	delay(500);
    wiz_Init(ip_addr); //initialize the wiznet chip
	while(1){  // Loop forever
		socketXstatus=wizGetCtl8X(whichsocket,SnSR); //active socket status
		switch (socketXstatus){
			case SOCK_CLOSED: //initial condition
				socketX_init(whichsocket);	//initialize socket 0
				break;
			case SOCK_ESTABLISHED: //someone wants to talk to the server
				gotsocket=whichsocket;
				whichsocket^=1; //alternate sockets
				socketX_init(whichsocket); //prepare it to listen while we're busy
				handlesession(gotsocket);
				break;
			//following are cases where we have to reset and reopen the socket
			case SOCK_FIN_WAIT: case SOCK_CLOSING: case SOCK_TIME_WAIT:
			case SOCK_CLOSE_WAIT: case SOCK_LAST_ACK:
				wizCmdX(whichsocket,CR_CLOSE);
				break;
		}
		delay(100);
	}
}

After a few false starts it compiles but i’m not happy with it. I’ll probably try it in the morning but there may be a better overall approach. As a minimum I’ve changed the way the socket close/init gets triggered and i bet it’s not getting cleaned up properly.

Advertisements

From → web server

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: