创客百科

姿势共享,有节操无门槛参与的创客百科,创客动力之源 \ (^_^) /

用户工具

站点工具


arduino:libraries:ethernetudpremoteip

Ethernet:UDP CLASS

UDP.remoteIP()

描述

获取远程连接的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());
  }
 
}

返回主菜单

本页面的其他翻译:
arduino/libraries/ethernetudpremoteip.txt · 最后更改: 2016/12/25 22:15 (外部编辑)