EthernetUDP.beginPacket()
描述
初始化 UDP 封包并指定封包欲传送至的 IP 位址及埠编号。
语法
Udp.beginPacket(remoteIP, remotePort);
参数
remoteIP
:接收封包目的地的 IP 位址
remotePort
:接收封包目的地的埠编号,型态为 int
回传
无回传值
范例
#include <Ethernet.h> #include <EthernetUdp.h> // 输入本地端的 IP 位址 IPAddress ip(192, 168, 1, 177); // 要监听的本地端埠编号 unsigned int localPort = 8888; // 创建一个 EthernetUDP 物件,我们可以藉由这个物件收发 UDP 的封包 EthernetUDP Udp; void setup() { // 开始 Ethernet 和 EthernetUDP Ethernet.begin(NULL,ip); Udp.begin(localPort); } void loop() { Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); Udp.write("hello"); Udp.endPacket(); }
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.