Skip to content

Telnet?

February 3, 2016

 

One of the challenges of adapting the Bagels Game for the olduino is that it was meant to be played on a tty type terminal where you could see your earlier input and responses rather than a browser type session where each response overlays everything that preceded it.  I had thought about developing some JavaScript hoohah that would let me emulate a line mode terminal but then i started thinking about telnet.  Telnet is basically a line mode terminal running over TCP so there’s no reason the ethernet cards wouldn’t support it and, in fact, I’ve probably used telnet to debug the bagels server so i could see exactly what it sent without the browser interpreting the html.

I fired up the 1802 Olduino running the Bagels game to make sure it worked, then started a putty telnet session to the same ip address and port.  It worked like a champ except that the telnet session shut down right after the server sent a page.  I had foolishly assumed that telnet was transaction-oriented like the browser but it’s not – it expects the socket to stay connected for the duration of the session.  Still, the principle is sound and it’s just a small matter of programming now.

I would need a new framework for the game though.  The current game keeps a table of active IPs and when a request comes in it looks up the ip in the table to get the current state.  Using Telnet with a single socket I wouldn’t have to do that – there would only be one state to keep but I could only have one player at a time.  If I activate a bunch of sockets I’d have to scan them sequentially for activity and maintain the states in an indexed table.  Maybe I’ll start with one socket and a single user.

There are some simple examples for arduino – like this one.

UPDATE:
Continuing to play with telnet I’m still struggling to find a mental model that I can extend to multiple users without driving myself bonkers or making something that’s even MORE complicated than the web server.  I feel like it should be simple but it isn’t.  I could maybe come up with a blocking single user client – maybe that’s something to start with.  One immediate issue is spinning off the negotiation sequences that putty sends at the beginning of a session.  I think I can safely ignore them so I will:

void handlesession(){
	unsigned int rsize;
	static unsigned char Inst1[]=
		"I AM THINKING OF A 3 DIGIT NUMBER.\n\rTRY TO GUESS " //50
		"MY NUMBER AND I WILL GIVE YOU CLUES AS FOLLOWS:\n\r";
	static unsigned char Inst2[]=
		"...PICO - ONE DIGIT IS IN THE WRONG PLACE\n\r"
		"...FERMI - ONE DIGIT IS IN THE CORRECT PLACE\n\r"
		"...DOUGHNUTS - NO DIGIT IS CORRECT\n\r";
	sendconst(Inst1); sendconst(Inst2);
	while(wizGetCtl16(SnRX_RSR)==0)	printf(".");
	delay(5);
	rsize=wizGetCtl16(SnRX_RSR); //get the size of the received data
	printf("\nrsize=%d\n",rsize);
	if (recv0(buf,min(16,rsize))>0){
		send0(buf,min(16,rsize));
	}
	sendconst("OK!!\n\r>"); //send a response
	phexblk(buf,rsize); printf("\n\r");
	flush(rsize);	//get rid of the received data
}

void main(void){
	unsigned char socket0status,connectstatus=0,listening=0,prevstat=0xff;
    unsigned char ip_addr[] = {169,254,180,2};
	delay(500);
    wiz_Init(ip_addr); //initialize the wiznet chip
	while(1){  // Loop forever
		socket0status=wizGetCtl8(SnSR); //socket 0 status
		if (socket0status!=prevstat){
			printf("s0 stat change to %X\n\r",socket0status);
			prevstat=socket0status;
		}
		switch (socket0status){
			case SOCK_CLOSED: //initial condition
				socket0_init();	//initialize socket 0
				break;
			case SOCK_LISTEN:
				listening=1;
				break;
			case SOCK_ESTABLISHED: //someone wants to talk to the server
				if(connectstatus==0){
					connectstatus=1;
					thisip.l=getip();
					printf("Hello to: %d.%d.%d.%d\n",
							thisip.c[0],thisip.c[1],thisip.c[2],thisip.c[3]);
					sendconst("Oh Hello!\n\r");
				}
				handlesession();
				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:
				wizCmd(CR_CLOSE);
				break;
		}
		delay(100);
	}
}

run1802.5F(2000)
Done Wiznet W5500 Initialization on IP address 169.254.180.2
Gateway Address 169.254.180.1
s0 stat change to 0000
s0 stat change to 0014
s0 stat change to 0017
Hello to: 169.254.168.228
rsize=21
FFFB1FFFFB20FFFB18FFFB27FFFD001FF000000000000000
rsize=15
FFFE1FFFFE20FFFE18FFFE27FFFC001
rsize=3
FFFB24
rsize=3
FFFE24

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: