connected()
描述
判斷 Client 物件是否連線中。如果用戶端與伺服端的連線已經中斷了,但是 Client 物件中仍然有從伺服端傳送的資料而未被讀取的話,該 Client 物件仍被視為連線中。
語法
client.connected()
參數
無參數
回傳
boolean:如果該 Client 物件連線中回傳 true,反之回傳 false
範例
/*
這個範例連線到 "http://arduino.cc/",把該網頁下載後輸出到序列埠監控視窗
*/
// 函式庫
#include <GSM.h>
// PIN 碼
#define PINNUMBER ""
// APN 資料
#define GPRS_APN "GPRS_APN" // 用你的 APN 取代
#define GPRS_LOGIN "login" // 用你的使用者名稱取代
#define GPRS_PASSWORD "password" // 用你的密碼取代
// 初始化函式庫
GSMClient client;
GPRS gprs;
GSM gsmAccess;
// 網址(URL)
char server[] = "arduino.cc";
char path[] = "/";
int port = 80;
void setup()
{
// 初始化 Serial
Serial.begin(9600);
Serial.println("Starting Arduino web client.");
// 連線狀態
boolean notConnected = true;
// 啟動 Arduino GSM shield,如果你的 SIM 卡有 PIN 碼,請當作 begin() 的參數
while(notConnected)
{
if((gsmAccess.begin(PINNUMBER)==GSM_READY) &
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("connecting...");
// 檢查是否成功連線
if (client.connect(server, port))
{
Serial.println("connected");
// HTTP 請求
client.print("GET ");
client.print(path);
client.println(" HTTP/1.0");
client.println();
}
else
{
// 如果沒有成功連線,印出錯誤訊息
Serial.println("connection failed");
}
}
void loop()
{
// 如果伺服端有傳送資料,讀取資料並且輸出到序列埠監控視窗
if (client.available())
{
char c = client.read();
Serial.print(c);
}
// 如果與伺服端的連線中斷了,關閉用戶端
if (!client.available() && !client.connected())
{
Serial.println();
Serial.println("disconnecting.");
client.stop();
// 不做任何事
for(;;)
;
}
}
See also
- GSMClient
- ready()
- connect()
- beginWrite()
- write()
- endWrite()
- read()
- available()
- peek()
- flush()
- stop()
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.
