connected()
描述
判断 Client
物件是否连线中。如果用户端与伺服端的连线已经中断了,但是 Client
物件中仍然有从伺服端传送的资料而未被读取的话,该 Client
物件仍被视为连线中。
语法
client.connected()
参数
无参数
回传
boolean
:如果该 Client
物件连线中则回传 true
,反之回传 false
范例
#include <WiFi.h> char ssid[] = "myNetwork"; // 无线网路的 SSID char pass[] = "myPassword"; // WPA 的密码 int status = WL_IDLE_STATUS; IPAddress server(74,125,115,105); // Google // 初始化 WiFiClient 类别 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(server, 80)) { Serial.println("connected"); // 传送 HTTP 封包 client.println("GET /search?q=arduino HTTP/1.0"); client.println(); } } } void loop() { if (client.available()) { char c = client.read(); Serial.print(c); } if (!client.connected()) { Serial.println(); Serial.println("disconnecting."); client.stop(); for(;;) ; } }
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.