Thank you for all of your help. This is more enough for me to get started! Though, I'm still having some problems with my code. My objectives are as follows:
- When a button is pressed, client connects to the server and sends "Q"
- Server responds with "A"
- Server sends disconnect message
- Client disconnects
My problem is that when the server calls client.stop(), my client thinks it's still connected, that is, while (client.connected()) still evaluates as true. I'm looking at the serial monitors and I see "Q" sent to the server, "A" received from the client, and the disconnect message is sent from the server, but the disconnect message is never received from the client.
So my question: If client.stop() is called from the server side, shouldn't client.connected() be evaluated as false on the client side?
Here's my code for the client:
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The ip address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,2);
const int led = 7;
const int button = 3;
const String serverIP = "192.168.1.1";
int buttonState = 0;
boolean isConnected = false;
// Enter the IP address of the server you're connecting to:
IPAddress server(192,168,1,1);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (Port 23 is default)
EthernetClient client;
void setup() {
// setup the pin mode for the LED & button
pinMode(led, OUTPUT);
pinMode(button, INPUT);
// start the Ethernet connection:
Ethernet.begin(mac, ip);
//Open serial communications and wait for the port to open:
Serial.begin(9600);
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.print("Will connect to ");
Serial.println(serverIP);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(button);
// if the button is pressed, connect to the server:
if (buttonState == HIGH && !isConnected) {
// if you get a connection, report back via serial,
// turn on the LED, and set the button flag:
if (client.connect(server,23)) {
isConnected = true;
digitalWrite(led, HIGH);
Serial.println("Server found");
client.print('Q');
}
else {
// if you didn't get a connection to the server:
Serial.println("Connection failed");
}
}
// if there are incoming bytes available
// from the sever, read them and print them
// to the serial queue and send them out to
// the socket if it's open:
while (client.connected()) {
while (client.available()) {
Serial.write(client.read());
Serial.println("while.available()");
}
Serial.println("while.connected()");
delay(1000);
}
// if the server's disconnected, stop the client
if (isConnected == true ) {
isConnected == false;
client.stop();
digitalWrite(led, LOW);
Serial.println("Stopping client");
}
delay(1000);
}
And here's the code for my server:
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };
IPAddress ip(192,168,1, 1);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);
const int led = 7;
// telnet defaults to port 23
EthernetServer server(23);
boolean alreadyConnected = false; // whether or not the client was connected previously
void setup() {
// setup the pin mode for the LED & button
pinMode(led, OUTPUT);
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.print("Chat server address:");
Serial.println(Ethernet.localIP());
}
void loop() {
// wait for a new client:
EthernetClient client = server.available();
// when the client send sends a question, provide an answer:
if (client) {
Serial.println("Client found");
if (!alreadyConnected) {
Serial.println("1st time client found");
// turn on status LED
digitalWrite(led, HIGH);
alreadyConnected = true;
}
// if there are incoming bytes available
// from a client, read them and print them
// to the serial queue and send them out to
// the socket if it's open:
while (client.available()) {
Serial.write(client.read());
Serial.println("while.available()");
}
delay(1000);
client.print('A');
}
// when client/server communication is complete, disconnect:
else if (alreadyConnected) {
//alreadyConnected = false;
digitalWrite(led, LOW);
Serial.println("Stopping client");
client.stop();
}
delay(1000);
}