available()

描述

取得一個與伺服端連線並且有資料要傳送給伺服端的用戶端。這個用戶端的連線並不會因為 Client 物件解構而中斷,如果要中斷連線可以呼叫 client.stop()

available() 是從 Stream 繼承而來。

語法


server.available()

參數

無參數

回傳

Client:如果沒有連線中的用戶端或是連線中的用戶端都沒有資料可以讀取,這個回傳的 Client 物件等同於 false(參考以下範例)

範例

#include <WiFi.h>

char ssid[] = "Network";          // 無線網路的 SSID
char pass[] = "myPassword";   // WPA 的密碼
int status = WL_IDLE_STATUS;

WiFiServer server(80);

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 {
    server.begin();
    Serial.print("Connected to wifi. My address:");
    IPAddress myAddress = WiFi.localIP();
    Serial.println(myAddress);

  }
}

void loop() {
  // 監聽是否有用戶端傳送請求
  WiFiClient client = server.available();
  if (client) {

    if (client.connected()) {
      Serial.println("Connected to client");
    }

    // 關閉連線
    client.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.