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
server.peek ()
Parametreler
Yok
İadeler
b: sonraki bayt veya karakter
-1: mevcut değilse
Misal
…
#include <SPI.h>
#include <WiFiNINA.h>
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
Serial.begin(9600);
while (!Serial) {
}
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
server.begin();
}
void loop() {
// listen for incoming clients
WiFiClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
int i = 0;
while (client.connected()) {
if (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++;
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
…
#include <SPI.h>
#include <WiFiNINA.h>
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
Serial.begin(9600);
while (!Serial) {
}
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
server.begin();
}
void loop() {
// listen for incoming clients
WiFiClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
int i = 0;
while (client.connected()) {
if (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++;
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
…