Click here to Skip to main content
15,888,279 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
/*
 This is code for 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 Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 */

#include <SPI.h>
#include <Ethernet.h>
#include <string.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, 2);
IPAddress myDns(0,0,0, 0);
IPAddress gateway(0, 0, 0, 0);
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);
  // 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 native USB port 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) {
      // clear out the input buffer:
      client.flush();
      alreadyConnected = true;
    }

    //input for user (us)
    //add string for this section
    if (client.available() > 0) {
      // read the bytes incoming from the client:
      char thisChar = client.read();
      //if not new line
      // echo the bytes back to the client:
      server.write(thisChar);
      //string += char
      // echo the bytes to the server as well:
      Serial.write(thisChar);
    }
  }
}




For the block of code at the bottom instead of reading and echoing bytes I would like to read lines and echo lines and also add the char to a string. But I do not know how, can someone please give some advice on how to do this ?

What I have tried:

Arduino Forums, Youtube and Stackoverflow forums.
Posted
Updated 20-Jan-19 1:44am

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900