用户工具

站点工具


reference:library:ethernetclient

Ethernet:客户端类

EthernetClient()

描述

创建一个可以连接到指定的互联网IP地址和端口的客户端(在client.connect()中定义)

语法

EthernetClient()

参数

例子

#include <Ethernet.h>
#include <SPI.h>
 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte server[] = { 64, 233, 187, 99 }; // Google 的服务器地址
EthernetClient client;
 
void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
    delay(1000);
  Serial.println("connecting...");
  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  } 
  else {
    Serial.println("connection failed");
  }
}
 
void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

返回主菜单

reference/library/ethernetclient.txt · 最后更改: 2017/04/07 10:03 (外部编辑)