write()


Description

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

Syntax

client.write(val)
client.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.

#include <Phpoc.h>

// hostname of web server:
char server_name[] = "example.phpoc.com";
PhpocClient client;

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

  Serial.println("Sending GET request to web server");

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

  // connect to web server on port 80:
  if(client.connect(server_name, 80))
  {
    // if connected:
    Serial.println("Connected to server");
    // make a HTTP request:
    //client.println("GET / HTTP/1.0");
    client.write("GET /asciilogo.txt HTTP/1.0\r\n");
    //client.write("GET /remote_addr/ HTTP/1.0\r\n");
    client.write("Host: example.phpoc.com\r\n");
    client.write("\r\n");
  }
  else // if not connected:
    Serial.println("connection failed");
}

void loop() {
  if(client.available())
  {
    // if there is an incoming byte from the server, read them and print them to
    // serial monitor:
    char c = client.read();
    Serial.print(c);
  }

  if(!client.connected())
  {
    // if the server's disconnected, stop the client:
    Serial.println("disconnected");
    client.stop();
    // do nothing forevermore:
    while(true)
      ;
  }
}