connect()
描述
连线到指定的 IP 位址与埠编号。除了使用 IP 位址与埠编号作为指定伺服器的方法,还可以使用网域名称(Domain Name)。
语法
client.connect(ip, port)
client.connect(URL, port)
参数
ip
:要连线的伺服器的 IP 位址(大小为 4 位元组的阵列)
URL
:要连线的伺服器的网域名称,型态为 string
(例:”www.86duino.com”)
port
:要连线的伺服器的埠编号,型态为 int
回传
boolean
:如果连线成功回传 true
,反之回传 false
范例
#include <WiFi.h> char ssid[] = "myNetwork"; // 无线网路的 SSID char pass[] = "myPassword"; // WPA 的密码 int status = WL_IDLE_STATUS; char servername[]="google.com"; // Google 的网域名称 WiFiClient client; void setup() { Serial.begin(9600); Serial.println("Attempting to connect to WPA network..."); Serial.print("SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); while(true); } else { Serial.println("Connected to wifi"); Serial.println("\nStarting connection..."); // 如果成功连线,透过序列埠印出讯息 if (client.connect(servername, 80)) { Serial.println("connected"); // 传送 HTTP 封包 client.println("GET /search?q=arduino HTTP/1.0"); client.println(); } } } void loop() { }
The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.