Serial not available until byte sent

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);
    }
  }
}

I guess I don't understand your explanation nor the question. If you are using the Arduino serial input, there is NOTHING available to your program until the serial port puts it in the buffer so your program can access it.

My ideal goal would be to have a server in which a client can connect and have a conversation with the Serial using text chat. To do this, the Serial would need to be able to send bytes to the client without the client having to send a byte to receive it. Currently, the serial can type, but the client does not receive the message. When the client sends a message to the serial, it updates and the text typed in the serial window appears on the client's screen. I am wondering how to have the Serial put the bytes in the buffer so the Serial.available command sees them.

Does not work that way unless you have logic errors in your program.

is it this?:
use server.accept() instead of serer.available()

you need to remember the client state from call to call of loop, e.g. make EthernetClient static
code then looks like

void loop() {
  static EthernetClient client=server.accept();
  int i=0;
  if (!client)
    client = server.accept();//available();
  else {
    if (!alreadyConnected) {
      //client.flush();
      Serial.println("We have a new client");
      client.println("Hello, client!");
      alreadyConnected = true;
    }

I changed my code to have EthernetClient as static and defined client prior to the if (!client) loop
Now, it prints the hello messages when the client joins (as expected), but no messages are able to be sent between the two. Does changing server to accept rather than available render the client and serial's availability status always zero, or is there a different issue?

this is the chatserver I used on aa Mega

/*
 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 <Ethernet.h>

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 176);
IPAddress myDns(192, 168, 1, 254);
IPAddress gateway(192, 168, 1, 254);
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(115200);
   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() {
  static EthernetClient client=server.accept();
  int i=0;
  if (!client)
    client = server.accept();//available();
  else {
    if (!alreadyConnected) {
      //client.flush();
      Serial.println("We have a new client");
      client.println("Hello, client!");
      alreadyConnected = true;
    }
    i=0;
    while (Serial.available() > 0)
    { 
      String thisChar2 = Serial.readStringUntil("\n");
      client.println(thisChar2);    
    }
    while (client.available()) 
    {
      char thisChar = client.read();
      Serial.print(thisChar);
    }
  }
}

the serial monitor displays

Chat server address:192.168.1.176
We have a new client
hello client test 1
hello client test 2
byeclient

the chatclient displays

F:\temp\chatSept2020>java ChatClient
chatClient attempting to connect to chatServer
connected !!
Received from server: Hello, client!
hello client test 1
Transmitting to server: hello client test 1
hello client test 2
Transmitting to server: hello client test 2
Received from server: hello server test 1
Received from server:
Received from server: hello server test 2
Received from server:
Received from server: bye server
Received from server:
byeclient
Transmitting to server: byeclient

the chatclient code (Java runs on a PC or Raspberry Pi)

// ChatClient.java - simple chat client - connects to charServer using TCP
//
// run charServer first then chatClient
import java.io.*;
import java.net.Socket;
import java.util.*;

public class ChatClient
{
   public static void main(String[] args) throws IOException
   {
      System.out.println("chatClient attempting to connect to chatServer");
      Scanner cin = new Scanner(System.in);      // open keyboard input
      Socket s = new Socket("192.168.1.176",23 );
      System.out.println("connected !!");
      Scanner in=new Scanner(s.getInputStream());
      PrintWriter out = new PrintWriter( s.getOutputStream()); 
      // thread to receive data from remote server
      Thread inthread = new Thread(){
        public void run(){
          while(true){
            String line = in.nextLine();
            System.out.println("Received from server: " + line);
            }
          }
       };
      inthread.start();
      // loop to read from ketboard and transmit to server
      while(true) {
            String response = cin.nextLine();  
            System.out.println("Transmitting to server: " + response);
            out.println(response);
            out.flush();
           }
   }

}

Hmm, that's strange. When I run the exact same code in your post, there is no communication between the serial and the client. It's probably because I am using a Teensy board, and therefore the Teensyduino interface to program it. Most of the time, it acts like a normal Arduino, but I have to use libraries like NativeEthernet rather than Ethernet. When using Ethernet, it reports no ethernet shield connection. It is awfully strange that this code (which is supposed to work) doesn't, and I'm not really sure why. I appreciate the help, though

I changed the IP addresses to suit my network - did you change them back?
does it compile, load, start, accept a client and then do nothing ? text sent from client does not appear on server and text from server does not appear on client ?
which Ethernet board are you using?

I can't believe I forgot to do that - it now works! :smiley: Thank you so much for the help!!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.