这里会显示出您选择的修订版和当前版本之间的差别。
— |
en:arduino:libraries:serveravailable [2016/12/25 22:15] (当前版本) |
||
---|---|---|---|
行 1: | 行 1: | ||
+ | [[en:arduino:libraries:ethernet|Ethernet]] : Server class | ||
+ | ====== available() ====== | ||
+ | |||
+ | ===== Description ===== | ||
+ | |||
+ | Gets a client that is connected to the server and has data available for reading. The connection persists when the returned client object goes out of scope; you can close it by calling client.stop(). | ||
+ | |||
+ | available() inherits from the Stream utility class. | ||
+ | ===== Syntax ===== | ||
+ | |||
+ | server.available() | ||
+ | ===== Parameters ===== | ||
+ | |||
+ | None | ||
+ | ===== Returns ===== | ||
+ | |||
+ | a Client object; if no Client has data available for reading, this object will evaluate to false in an if-statement (see the example below) | ||
+ | ===== Example ===== | ||
+ | <code cpp> | ||
+ | #include <Ethernet.h> | ||
+ | #include <SPI.h> | ||
+ | |||
+ | // the media access control (ethernet hardware) address for the shield: | ||
+ | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; | ||
+ | //the IP address for the shield: | ||
+ | byte ip[] = { 10, 0, 0, 177 }; | ||
+ | // the router's gateway address: | ||
+ | byte gateway[] = { 10, 0, 0, 1 }; | ||
+ | // the subnet: | ||
+ | byte subnet[] = { 255, 255, 0, 0 }; | ||
+ | |||
+ | |||
+ | // telnet defaults to port 23 | ||
+ | EthernetServer server = EthernetServer(23); | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | // initialize the ethernet device | ||
+ | Ethernet.begin(mac, ip, gateway, subnet); | ||
+ | |||
+ | // start listening for clients | ||
+ | server.begin(); | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | // if an incoming client connects, there will be bytes available to read: | ||
+ | EthernetClient client = server.available(); | ||
+ | if (client == true) { | ||
+ | // read bytes from the incoming client and write them back | ||
+ | // to any clients connected to the server: | ||
+ | server.write(client.read()); | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | [[en:arduino:libraries|Reference Home]] |