Figured it out myself as usual
![Tweak :-/O](https://www.eevblog.com/forum/Smileys/default/smiliey_cal.gif.pagespeed.ce.yYMcAvxBJk.gif)
I appreciate the open-source community and for those who contribute , I don't want somebody with the same question to stumble upon this thread only to hit a dead end so i'm posting the successful code below:
Simple chat server with command feature , reads client.read() and stores data into string 'inData' then echos string to server (to confirm we have it). If client types "help" in the server, it back specific text to 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.
// gateway and subnet are optional:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 4);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 255, 0);
// telnet defaults to port 23
EthernetServer server(23);
boolean alreadyConnected = false; // whether or not the client was connected previously
String inData;
void setup() {
// 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);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Chat server address:");
Serial.println(Ethernet.localIP());
}
void loop() {
// wait for a new client:
EthernetClient client = server.available();
// when the client sends the first byte, say hello:
if (client) {
if (!alreadyConnected) {
// clead out the input buffer:
client.flush();
Serial.println("We have a new client");
client.println("Hello, client!");
alreadyConnected = true;
}
if (client.available() > 0) {
// read the bytes incoming from the client:
char thisChar = client.read();
inData += thisChar;
// echo the bytes back to the client:
server.write(thisChar);
// echo the bytes to the server as well:
Serial.write(thisChar);
// echo to the server what's been received to confirm we have the string
if (thisChar == '\n')
{
Serial.print("\nreceived:");
Serial.print(inData);
inData = "";
}
// if the string is "help" then return message to client
if (inData == "help") {
server.write("\nYou said help!\n");
}
}
}
}