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.