Ethernet:UDP CLASS
获取远程连接的IP地址。
这个函数必须调用在 UDP.parsePacket()之后。
UDP.remoteIP();
无
4个字节:远程连接的IP地址
#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()); } }