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.