I was trying to write a Telnet server that would be able to accept commands of sorts using the Arduino Ethernet library, and in order to access what the client was saying (to compare it to strings to check what command the client entered), I made a buffer string that would read until \n
, which is part of the standard Telnet library. However, the string would never get printed to the serial connection (just for debugging purposes). Here's my code:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xFE, 0xED }; // this client's MAC address
EthernetServer server(23); // telnet is 23
boolean alreadyConnected = false;
void setup() {
Ethernet.begin(mac);
server.begin(); // open the socket
Serial.begin(9600);
while (!Serial) {
; // wait for serial to start, Leonardo only
}
Serial.print("Connect to ");
Serial.print(Ethernet.localIP());
Serial.println(":23.");
}
void loop() {
EthernetClient c = server.available();
if (c) {
String messageBuffer = "";
if (!alreadyConnected) {
c.flush();
Serial.println("New client");
c.println("test post, please ignore");
alreadyConnected = true;
}
if (c.available() > 0) {
char thisChar = c.read();
//server.write(thisChar);
//buffer until the client sends a new line
if (thisChar == '\n') {
Serial.println(messageBuffer);
messageBuffer = "";
} else {
messageBuffer += thisChar;
}
}
}
}
Most of this code was copied from the Telnet chat server example, but I changed the Serial.write(thisChar)
line, because I wanted to put it into a string first. When this is run, and a user telnet
s to the IP, the Arduino prints New client
, but when the user sends data, it doesn't print anything.
Any suggestions for what I'm doing wrong?