write()


Description

Writes data to all the clients connected to a server.
This data is sent as a byte or series of bytes.

Syntax

server.write(val)
server.write(buf, len)

Parameters

val - a value to send as a single byte (byte of char)
buf - an array to send as a series of bytes (byte or char)
len - the length of the buffer

Returns

Returns an int represents the number of bytes written.

Example

#include <Phpoc.h>

PhpocServer server(23);
boolean alreadyConnected = false; // whether or not the client was connected previously

void setup() {
  Serial.begin(9600);
  while(!Serial)
    ;

  // initialize PHPoC [WiFi] Shield:
  Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
  //Phpoc.begin();

  // start listening for TCP clients:
  server.begin();

  // print IP address of PHPoC [WiFi] Shield to serial monitor:
  Serial.print("Chat server address : ");
  Serial.println(Phpoc.localIP());
}

void loop() {
  // wait for a new client:
  PhpocClient client = server.available();

  // when the client sends the first byte, say hello:
  if (client) {
    if (!alreadyConnected) {
      // clear out the transmission 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 thisChar = client.read();
      // echo the bytes back to all connected clients:
      server.write(thisChar);
      // echo the bytes to the server as well:
      Serial.write(thisChar);
    }
  }
}