Komut
peek()
Açıklama
Bir sonrakine geçmeden dosyadan bir bayt okuyun. Yani, art arda peek () çağrıları, sonraki okuma () çağrısıyla aynı değeri döndürür.
Bu işlev Stream sınıfından miras alındı. Daha fazla bilgi için Stream sınıfı ana sayfasına bakın.
Sözdizimi
Client.peek ()
Parametreler
Yok
İadeler
b: sonraki bayt veya karakter
-1: mevcut değilse
Misal
…
#include <SPI.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
char server[] = "www.google.com"; // name address for Google (using DNS)
WiFiClient client;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; }
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
while (true);
}
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
Serial.println("Connected to wifi");
printWifiStatus();
Serial.println("\nStarting connection to server...");
IPAddress result;
int err = WiFi.hostByName(server, result) ;
if (err == 1) {
Serial.print("Ip address: ");
Serial.println(result);
} else {
Serial.print("Error code: ");
Serial.println(err);
}
if (client.connect(result, 80)) {
Serial.println("connected to server");
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
}
}
void loop() {
int i = 0;
while (client.available()) {
char c = client.peek();
Serial.print("peek: ");
Serial.println(c);
Serial.print("calling a second time peek, the char is the same: ");
c = client.peek();
Serial.println(c);
Serial.print("calling the read retry the char and erase from the buffer: ");
c = client.read();
Serial.println(c);
if (i == 2) {
while (1);
}
i++;
}
}
…
#include <SPI.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
char server[] = "www.google.com"; // name address for Google (using DNS)
WiFiClient client;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; }
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
while (true);
}
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
Serial.println("Connected to wifi");
printWifiStatus();
Serial.println("\nStarting connection to server...");
IPAddress result;
int err = WiFi.hostByName(server, result) ;
if (err == 1) {
Serial.print("Ip address: ");
Serial.println(result);
} else {
Serial.print("Error code: ");
Serial.println(err);
}
if (client.connect(result, 80)) {
Serial.println("connected to server");
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
}
}
void loop() {
int i = 0;
while (client.available()) {
char c = client.peek();
Serial.print("peek: ");
Serial.println(c);
Serial.print("calling a second time peek, the char is the same: ");
c = client.peek();
Serial.println(c);
Serial.print("calling the read retry the char and erase from the buffer: ");
c = client.read();
Serial.println(c);
if (i == 2) {
while (1);
}
i++;
}
}
…
Ayrıca bakınız
- Client
- WiFiClient()
- WiFiSSLClient()
- connected()
- connect()
- connectSSL()
- status()
- write()
- print()
- println()
- available()
- read()
- flush()
- stop()
- remoteIP()
- remotePort()