用户工具

站点工具


reference:library:ethernetudpremoteip

差别

这里会显示出您选择的修订版和当前版本之间的差别。


reference:library:ethernetudpremoteip [2023/06/07 04:24] (当前版本) – 创建 - 外部编辑 127.0.0.1
行 1: 行 1:
 +[[zh:reference:librariy:ethernet|Ethernet]]:UDP CLASS
 +====== UDP.remoteIP() ======
 +
 +===== 描述 =====
 +
 +获取远程连接的IP地址。
 +
 +这个函数必须调用在 UDP.parsePacket()之后。
 +===== 语法 =====
 +
 +UDP.remoteIP(); 
 +===== 参数 =====
 +
 +
 +===== 返回 =====
 +
 +4个字节:远程连接的IP地址
 +===== 例子 =====
 +<code cpp>
 +#include <SPI.h>
 +#include <Ethernet.h>
 +#include <EthernetUdp.h>
 +
 +//为你的控制器输入MAC地址和IP地址。
 +// IP地址将依赖于你的本地网络:
 +byte mac[] = {  
 +  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
 +IPAddress ip(192, 168, 1, 177);
 +
 +unsigned int localPort = 8888;      //本地监听端口
 +
 +// EthernetUDP的实例,让我们通过UDP发送和接收数据包
 +EthernetUDP Udp;
 +
 +void setup() {
 +  //启动以太网和UDP:
 +  Ethernet.begin(mac,ip);
 +  Udp.begin(localPort);
 +}
 +
 +void loop() {
 +
 +  int packetSize = Udp.parsePacket();
 +  if(packetSize)
 +  {
 +    Serial.print("Received packet of size ");
 +    Serial.println(packetSize);
 +    Serial.print("From IP : ");
 +
 +    IPAddress remote = Udp.remoteIP();
 +    //打印出的远程连接的IP地址
 +    Serial.print(remote);
 +
 +    Serial.print(" on port : ");
 +    //打印出远程连接的端口
 +    Serial.println(Udp.remotePort());
 +  }
 +
 +}
 +</code>
 +
 +[[zh:reference:librariy:main|返回主菜单]]
 +
 +