Hello,
I am currently attempting to code a TCP Server using a simple bit of modified example code, included below. Using PuTTY to connect, it mostly works. I say mostly because the Serial only registers as "available" when the client sends a byte. For example, I can type something in the Serial input, and it'll only show up on the client-side when a byte is sent from there.
How do I get it to register the available bytes immediately?
Thanks!
Code
/*
Chat Server
A simple server that distributes any incoming messages to all
connected clients. To use, telnet to your device's IP address and type.
You can see the client's input in the serial monitor as well.
Using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
*/
#include <SPI.h>
#include <NativeEthernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 2, 176);
IPAddress myDns(192, 168, 2, 5);
IPAddress gateway(192, 168, 2, 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
void setup() {
// initialize the ethernet device
Ethernet.begin(mac, ip, myDns, gateway, subnet);
Serial.begin(9600);
while (!Serial) {
;
}
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1);
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start listening for clients
server.begin();
Serial.print("Chat server address:");
Serial.println(Ethernet.localIP());
}
void loop() {
int i=0;
EthernetClient client = server.available();
if (client) {
if (!alreadyConnected) {
client.flush();
Serial.println("We have a new client");
client.println("Hello, client!");
alreadyConnected = true;
}
// client.println("Working...");
i=0;
while (Serial.available() > 0)
{
char thisChar2 = Serial.read();
client.print(thisChar2);
}
i=0;
while (client.available())
{
char thisChar = client.read();
Serial.print(thisChar);
}
}
}