0

我有一个带有 Ethernet Shield 的Arduino Uno作为服务器,我通过 Internet 在 Arduino 上提出请求。我使用了两个库(Ethernet.h 和 SPI.h)。

我想检查客户端IP 地址,所以我只接受来自已知 IP 地址(例如 50.50.50.50)的HTTP请求,这是我办公室的静态 IP 地址。如何获取 Arduino 上的客户端 IP 地址?

4

3 回答 3

4

看看以下内容,这适用于 TCP:

http://forum.arduino.cc/index.php?PHPSESSID=jh6t8omt7vrb8nget5c9j5dbk4&/topic,82416.0.html

以下是引用作者的帖子,我只是复制优秀作品:

为了使它工作,我做了以下工作:

我在 EthernetClient.cpp 文件的末尾添加了以下几行:

uint8_t *EthernetClient::getRemoteIP(uint8_t remoteIP[])
{
  W5100.readSnDIPR(_sock, remoteIP);
  return remoteIP;
}

然后,我在 EthernetClient.h 文件中添加了以下行(在 virtual void stop(); 行下):

uint8_t *getRemoteIP(uint8_t RemoteIP[]);//adds remote ip address

最后,我在我的草图中使用了以下代码来访问远程 IP:

client.getRemoteIP(rip); // where rip is defined as byte rip[] = {0,0,0,0 };

为了在串行监视器中显示 IP,我使用了:

for (int bcount= 0; bcount < 4; bcount++)
     {
        Serial.print(rip[bcount], DEC);
        if (bcount<3) Serial.print(".");
     }
于 2013-09-29T19:45:18.730 回答
1

我已经使用UDP完成了这个,希望这会对你有所帮助。

在此处从 Google 获取 UDP.h:UDP.h

代码:

#include <SPI.h>
#include <Ethernet.h>
#include <Udp.h> 

// *****  ETHERNET VARS *****
// MAC address and IP for arduino
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,98};
unsigned int localPort = 8888;      // local port to listen on

// SenderIP and SenderPort are set when message is received
byte SenderIP[IP_LENGTH];        // holds received packet's originating IP
unsigned int SenderPort;        // holds received packet's originating port

// buffer for receiving data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet
int packetSize = 0;

void setup() 
{
  Ethernet.begin(mac,ip);  //start Ethernet
  Udp.begin(localPort);    //start UDP
}

void loop()
{
  if(NewPortMessage())
  {
      // Do stuff, SenderIP is the IP where the UDP message was received from
  }
}

boolean NewPortMessage()
{
  packetSize = Udp.available();
  if(packetSize > 0)
  {
    packetSize -= 8; //subtract UDP 8-byte header
    // read the packet into packetBufffer and get the senders IP addr and port number
    Udp.readPacket(packetBuffer,UDP_TX_PACKET_MAX_SIZE, SenderIP, SenderPort);
    return true;
  }
  clearPacketBuffer();
  return false;
}

void clearPacketBuffer()
{
  for(int i=0; i < packetSize; i++)
    packetBuffer[i] = 0;
}
于 2013-01-04T19:58:48.077 回答
0

改变方法呢?你可以使用 SOA,你可以让你的 arduino 成为一个 web 客户端而不是 web 服务器......然后你可以在托管你的 web 服务的 web 服务器中处理所有这些限制,这个 web 服务将是核心你的应用程序,这样你就可以从任何你想要的移动设备上调用它:D

只是一个想法 arduino 网络服务器不是很有用,通过这种方法,您可以使用互联网而不是仅使用 LAN

祝你的项目好运

于 2013-01-08T00:36:19.883 回答