client.read() returns single byte.
https://www.arduino.cc/reference/en/libraries/ethernet/client.read/
A simple test sketch based on “Chat Server” example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/* based on Chat Server example */ #include <SPI.h> #include <Ethernet.h> byte mac[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // change to your own IPAddress ip(192, 168, 1, 177); EthernetServer server(23); // telnet defaults to port 23 bool alreadyConnected = false; // whether or not the client was connected previously int count = 0; void setup() { Ethernet.begin(mac, ip); Serial.begin(9600); if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); while (true) { delay(1); // do nothing, no point running without Ethernet hardware } } server.begin(); Serial.print("Chat server address:"); Serial.println(Ethernet.localIP()); } void loop() { EthernetClient client = server.available(); if (client) { if (!alreadyConnected) { // clear 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 buf = client.read(); // echo the bytes back to the client: server.write(buf); server.write(count); // echo the bytes to the Serial monitor as well: Serial.println(buf); Serial.println(count); count += 1; } } } |
One gets following Serial monitor after sending a set of characters “ABCD” by, for example, socket.send(“ABCD”.encode(“utf-8”)) in a python script.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Chat server address:192.168.1.177 We have a new client A 0 B 1 C 2 D 3 4 |