connect()


Description

Connects to a server with specified IP address( or hostname) and port.

Syntax

client.connect(ip_addr, port)
client.connect(hostname, port)

Parameters

ip_addr - the IP address which the client will connect to
port - the port number that the client will connect to
hostname - the hostname that the client will connect to

Returns

1 - on success
0 - on failure

Example

#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.println("GET /asciilogo.txt HTTP/1.0");
    //client.println("GET /remote_addr/ HTTP/1.0");
    client.println("Host: example.phpoc.com");
    client.println();
  }
  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)
      ;
  }
}