Hi All,
I have an Uno R3 and a W5100-based Ethernet/microSD shield. I purchased both in July 2015. I assume the Ethernet shield is a recent revision but I don't know.
I am developing a sketch which will run a "telnet server" on my Uno. I aim to be able to telnet to the Uno and issue a command, probably one character. The Arduino will respond with some data and then disconnect me. Really simple. I am developing on a Mac running OSX Yosemite and I am using the most recent Arduino IDE. My Mac and the Uno/ES are plugged into the same little Netgear switch at my house. Nothing fancy. I am testing with plain old OSX "telnet" running from a plain old Terminal.app.
(Note that this is my first time to use the Ethernet shield, although not my first Arduino sketch and not my first time with "light" client/server network programming (TCL, Perl). For what that's worth )
My problem is that I cannot get client.stop to disconnect my telnet session. Well, I can, but only if I put the call in a certain place in the sketch. But I cannot if I put it where I want to put it, which should be functionally equivalent, but evidently is not.
My problem is similar to a side problem in this thread, but the side problem is only mentioned at the end and it is ignored because the OP was so happy his main problem was solved, I guess
I have developed a sketch to illustrate my problem as plainly as I can. In fact, I started with some good, solid code from the thread I referenced. Anyway, here is my example:
#include <SPI.h>
#include <Ethernet.h>
boolean alreadyConnected = false;
boolean forceDisconnect = false;
int i = 0;
int j = 0;
int timeout = 15000; // give server time to come up.
byte pin_SDCardCS = 4;
EthernetServer server(23);
void setup() {
Serial.begin( 9600 );
delay( 1000 );
Serial.println( "serial started" );
delay( 1000 );
pinMode( pin_SDCardCS, OUTPUT );
digitalWrite( pin_SDCardCS, HIGH );
byte mac[] = { 0x00, 0x08, 0xDC, 0x00, 0x00, 0x42 }; // WizNet OUI is 00:08:DC .
IPAddress ip(192,168,177,105);
IPAddress gw(192,168,177,1);
IPAddress sm(255,255,255,0);
Ethernet.begin( mac,ip,gw,sm );
server.begin();
}
void loop() {
if( i < timeout ) {
i++;
} else {
forceDisconnect = true;
}
EthernetClient client = server.available();
if( client ) {
Serial.println( "\nyes server.available" );
if( !alreadyConnected ) {
Serial.println( "\nnew client" );
alreadyConnected = true;
} else {
Serial.println( "\nsame old client" );
}
while( client.available() ) {
if( forceDisconnect ) {
Serial.println( "\nforce disconnecting client inside" );
client.stop();
delay( 1000 );
alreadyConnected = false;
forceDisconnect = false;
i = 0;
timeout = 5000; // can timeout faster now. but user needs to be quick with the telnet.
} else {
Serial.println( "same old client available" );
char c = client.read();
}
}
} else {
// we spend a lot of time here, so just output a dot. 100 dots per line.
// output i at the end of every line so we can keep track of time.
j++;
Serial.print( "." );
if( j > 99 ) {
j = 0;
Serial.println( i );
}
}
if( forceDisconnect ) {
if( alreadyConnected ) {
Serial.println( "\nforce disconnecting client outside" );
client.stop();
delay( 1000 );
alreadyConnected = false;
}
}
delay( 1 );
}
I run this code with the IDE serial monitor open for debugging.
If I telnet to the Uno/ES and hit enter a few times I can see the serial debug output indicating it sees the connection and my keystrokes. It also sees some initial telnet garbage ,which I assume is my telnet client attempting to negotiate with the server. No worries there.
However, when the counter reaches 15,000 and the sketch goes to disconnect (outside disconnect) me, it does not. It says it does, but my telnet session remains connected. The sketch only disconnects me if I hit enter after the counter reaches 15,000 (inside disconnect).
It's like the client object is not "hot" or "selected" when the outside disconnect fires. The client object is only "selected" immediately after reading data from it. Or maybe it is only "visible" in the scope where a client.available() is called. Or maybe it is a problem with my code. I am inclined to think this is in fact the case, but I just can't find it.
I have tinkered with this for hours with no luck. Could someone out there please have a look and help me see what I am doing wrong? If any clarification is needed please let me know.
-cls