write()
描述
傳送資料到目前與伺服端連線的所有用戶端。
語法
server.write(val)
server.write(buf, len)
參數
val:要傳送到用戶端的資料,大小為一個位元組,型態可以為 byte 或 char
buf:一個含有要傳送給用戶端的資料的陣列,型態可以為 byte 或 char
len:要傳送的資料陣列的大小
回傳
byte:回傳 write() 傳送至用戶端的位元組數量
範例
#include <Ethernet.h>
// 網路設定。通訊閘和子網路遮罩可選擇給或不給
byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
// IP 位址
byte ip[] = { 10, 0, 0, 177 };
// 路由器的通訊閘位址
byte gateway[] = { 10, 0, 0, 1 };
// 子網路遮罩
byte subnet[] = { 255, 255, 0, 0 };
// telnet 預設在埠編號 23
EthernetServer server = EthernetServer(23);
void setup()
{
// 初始化網路裝置
Ethernet.begin(mac, ip, gateway, subnet);
// 開始監聽是否有連線請求
server.begin();
}
void loop()
{
// 如果有連線的請求,將會得到一個 Client 的物件
EthernetClient client = server.available();
if (client == true) {
// 從目前連線的用戶端讀取資料,並且回傳到所有目前與伺服端連線的用戶端
server.write(client.read());
}
}
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.
