write()

描述

传送资料到目前与伺服端连线的所有用户端。

语法


server.write(val)
server.write(buf, len)

参数

val:要传送到用户端的资料,大小为一个位元组,型态可以为 bytechar
buf:一个含有要传送给用户端的资料的阵列,型态可以为 bytechar
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.