How to obtain the remote client IP address when using the Ethernet Shield

Zoomcat,

You've not put the client.getRemoteIP(rip); line in to actually read the remote IP prior to printing it.

[color=red]client.getRemoteIP(rip); // this is the code that actually puts the IP into the variable rip[/color]
for (int bcount= 0; bcount < 4; bcount++)
     { 
        Serial.print(rip[bcount], DEC); 
        if (bcount<3) Serial.print("."); else Serial.println();
     }

I am a super newbee. I am not sure where to connected the ethernet cable if I want to get remote data from home. The router?

Added the client.getRemoteIP(rip); line, and voila!, it works.

samlro2:
I am a super newbee. I am not sure where to connected the ethernet cable if I want to get remote data from home. The router?

samlro2, your question isn't directly related to this topic, but I will help you out.

If you want to connect to your Arduino via a network or internet you will need to plug it into a router.

Then, from within the network, you can access the arduino simply by entering the arduino's IP into a browser (the IP you set within the sketch).

Connecting via the internet, things get a little more complicated:

  • First you will need to set up your router so it routes all port 80 requests to the arduino. google port forwarding and your router model.
  • Second you will need some way of contacting your router from the internet, your external IP address is likely to vary, so the best way is to get a domain name. google about dynamic domain name providers (i.e. dyndns, no-ip etc).

There are a few guides that go into a lot more detail and give full sketches. Google should be able to help there.

I spend a lot of time to find a solution of error

no matching function for call to 'EthernetClient::getRemoteIP(byte* [4])'

The right way is to replace the line of code byte *rip[4];
with uint8_t rip[4];

Really thanks for the new Ethernet library, but why is the example wrong?

So there is an error on the 164 line in file EthernetClient.cpp too, the line must be

uint8_t *EthernetClient::getRemoteIP(uint8_t *remoteIP)

Could this solve your problem?

Discussion that might be of interest.

http://forum.arduino.cc/index.php/topic,82416.0.html

Even if this discussion is old, it is still relevant.

Improvement:

EthernetClient.cpp

IPAddress EthernetClient::getRemoteIP()
{
  byte rip[4];
  W5100.readSnDIPR(_sock, rip);
  return rip;
}

EthernetClient.h

IPAddress getRemoteIP(); // get Remote IP address

Reason:
You don't need to use some pass by reference and use return combined, that is redundant.
Here you just use return value.

Example:

  EthernetClient client = server.available();

  if (client)
  {
    Serial.println("new client");
    
    IPAddress remoteip = client.getRemoteIP();
Serial.print("IP: ");
    Serial.println(remoteip);
    client.stop();
}

Regards.

@karlok returning a pointer declared in the local scope of a function is not the right thing to do.
The "pass by reference" method is the right one.

giampiero7:
@karlok returning a variable declared in the local scope of a function is not the right thing to do.
The "pass by reference" method is the right one.

+1

Or you could qualify the local "rip" as "static".
This isn't ideal either, though.

Why is this code not incorporated into the Ethernet library? It's very short but quite helpful!

hilltop:
Why is this code not incorporated into the Ethernet library? It's very short but quite helpful!

See: Enhancement: add operator==, localPort, remoteIP and remotePort to EthernetClient by ntruchsess · Pull Request #1700 · arduino/Arduino · GitHub
As for why was this function not accepted in the pull request, I've read the associated discussion but still don't understand. I get that compatibility with the Client API is essential but why can't additional functions be added? If you want an easy way to add the remoteIP() function to the stock Ethernet library then you might find this library useful: GitHub - Chris--A/EthernetClientEx: An extension to the Arduino default EthernetClient library.
Note that remoteIP() in the pull request and the EthernetClientEx library as well as EthernetUDP all use an IPAddress return type instead of the "pass by reference" method.

Hi! :slight_smile:

So is it to be understood,that it is still not possible to use the remoteIP() in official releases on incoming TCP connections, and somewhere someone is still pondering if it is I good idea to implement it, but no one can tell where it lands?

And that there are ways to implement it yourself, but it was loosely discussed which way was best and that didn't got settled either ?

I'm puzzled !

Best regards

fxfever:
So is it to be understood,that it is still not possible to use the remoteIP() in official releases on incoming TCP connections

Correct, you need to either modify your Ethernet library(as explained in this thread), install+use EthernetClientEx, or install a library that's already modified for you.

fxfever:
, and somewhere someone is still pondering if it is I good idea to implement it, but no one can tell where it lands?

From the discussion on the attempt to add this function to the stock Ethernet library it seems it was definitely decided against, though I don't understand why. Of course it's been a while so it might be reconsidered if someone brought it up with good reasoning why the previous decision is wrong.

fxfever:
there are ways to implement it yourself, but it was loosely discussed which way was best and that didn't got settled either ?

I use the IPAddress return type because that's how it was done in the official EthernetUDP remoteIP() and is more user friendly but the "pass by reference" method will work fine also.

Thanx for the sum up !
Must do the work around then - since I have no idea how to get anyone to consider anything around here ! :slight_smile:

Add this to your sketch (do not change Ethernet library files):

extern "C" {
#include "utility/w5100.h"
}

void getRemoteIP(EthernetClient *client, uint8_t *ptRemoteIP)
{
W5100.readSnDIPR(client->getSocketNumber(), ptRemoteIP);
}

..
uint8_t remoteIP[4];
getRemoteIP(client,remoteIP);
..

Thanks for this, it seems like a lot more civilized way of reaching in to see the remote IP address.
The get the result out I had to use:

getRemoteIP(&client,remoteIP);

rather than

getRemoteIP(client_1,remoteIP);

krasimir:
Add this to your sketch (do not change Ethernet library files):

extern "C" {
#include "utility/w5100.h"
}

void getRemoteIP(EthernetClient *client, uint8_t *ptRemoteIP)
{
W5100.readSnDIPR(client->getSocketNumber(), ptRemoteIP);
}

..
uint8_t remoteIP[4];
getRemoteIP(client,remoteIP);
..

Hello all, this is my first post. I've been working to add this remote IP functionality to my project. All the suggestions here and elsewhere have not worked. My project uses an UNO with an Ethernet2 shield. The original Ethernet libray worked fine except for the remote IP functionality. So, I dowmloaded the Ethernet2 library from somewhere and it did not have any code pertaining to remote IP. So I started poking at it with a stick. I got it to work today, finally. Here's what I did:

Ethernet2 Library Modifications

Modifications made to two files:

...\Arduino\libraries\Ethernet2-1.0.4\src\ EthernetClient.cpp

I added the follow bit of code to the very end of the file:

// Added by Stephen Kurth Feb 8, 2019
// returns the remote IP address:
IPAddress EthernetClient::getRemoteIP()
{
if (_sock >= MAX_SOCK_NUM) return IPAddress((uint32_t)0);
uint8_t remoteIParray[4];
SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0));
w5500.readSnDIPR(_sock, remoteIParray);
SPI.endTransaction();
return IPAddress(remoteIParray);
}

I originallly pasted a copy of this code from the same .cpp file in the original Ethernet Library. Had to change a bunch of stuff in it though to get it compile. The shown code compiled and worked.

...\Arduino\libraries\Ethernet2-1.0.4\src\ EthernetClient.h

I added this code to this file in this location:

virtual bool operator!=(const EthernetClient& rhs) { return !this->operator==(rhs); };
// added by Stephen Kurth Feb 8, 2019
IPAddress getRemoteIP();

friend class EthernetServer;

Most of the other lines start with virtual but I don't really know what it does. By that point I was just poking it with a stick and seeing if it would compile. It did, finally.

The code in my Arduino sketch is:

client = server.available(); // try to get client

if (client) // got client?
{
IPAddress remoteIp = client.getRemoteIP();
Serial.print(F("Remote IP: "));
Serial.println(remoteIp);
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available()) // client data available to read
{

I made all the library edits in Visual Studio 2017.

I just thought I'd share in case anyone else is trying to do this with the same hardware.

Stephen_Kurth:
The original Ethernet libray worked fine except for the remote IP functionality.

What problem did you encounter with the remote IP functionality?

Hello pert,

To be honest, I can't really say why none of the suggestions were working for me... It is entirely probable that I was overlooking something, or made a mistake somewhere... In hindsight, I should have paid more attention to the compiler errors. Having edited the Ethernet 2 library to add the remote IP function, I suppose I could've done the same for the original library...

However, I am using the Ethernet 2 shield in my project so I thought it best to concentrate on a library written specifically for the w5500 chip. I had run into this issue with another sketch (did not involve remote IP) where the original Ethernet library did not work but the Ehternet 2 library did ...

As I am rethinking this, I wonder if I could've added a w5500.h utility file to the original library?..

One thing, among many, concerned me with the original Ethernet library. The EthernetClient.h file has nothing in it except an include line: #include "Ethernet.h". That's it. I don't know if that's an issue or not but the same file in the Ethernet 2 library has a ton more stuff in it.

BTW, I downloaded the Ethernet 2 library from Ethernet2 - Arduino Libraries

Sorry I can't give you a better answer, I'm just a guy poking at this with a stick. I'm amazed that I actually got this to work.