Go Down

Topic: Get IP Address of Anyone Connected to my Arduino set up as server (Read 8395 times) previous topic - next topic


I don't quite know what that does, I didn't write it. It was given a few posts back and at one time, I had it working, must have forgot to save my work after the last burn, and lost it. If there is a simpler idea, I'm open to it.

And what were you planning to achieve when that code was given to you? If you don't know what it does... and it is given you trouble. Delete it, and run the program. See if something's missing... if not, you just fixed it. ;)
This... is a hobby.


Well, when robtillaart gave that code bit, I wanted the clients IP written to a string that could be printed to the LCD. I'm not sure if the question was supposed to be sarcastic or not, but when I answered the question with "I don't quit know...", of course I know what I want it to do, BUT, I don't necessarily know what each line is actually doing.

So, I changed this...
Code: [Select]

           for (int i=0; i<3; i++)
              client_ip[i] = () temp & 0xFF; // I added the index thing here...
              temp /= 256;

So, I'm not sure what this is going to do when I upload it to my board, so is there a way to download the working previous version from my board, even if it's not pretty and upload it later if this breaks my program?


I'm running IDE 21 and I have the ip of the incoming printing just fine using code I took from this thread.

to Client.cpp I added:

void Client::getRemoteIP_address(uint8_t * addr){ // added by me
   W5100.readSnDIPR(_sock, addr);    // replaces the getSn_DIPR(_sock, addr); V18 and below

and to Client.h:

  void getRemoteIP_address(uint8_t * addr);//added by me

Then in my code to get the address:

  uint8_t clientIp[4];


to format the ip address so you can display it you can call the routine below:

// format an IP address. (I stole this)
const char* ip_to_str(const uint8_t* ipAddr)
  sprintf(Dbuf, Dbuf2, ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);  //see how its a 4 byte array?
  return (Dbuf);

Dbuf and Dbuf2 are string arrays declared at the beginning of the program and used as scratch buffers throughout.  Call the routine like this to see what you get:

Serial.println(clientIp);  // where clientIp is the result of the incoming.getRemoteIP_address above

I do not recommend converting the ip address to a long or anything else except a string for display.  Use it as a 4 byte array since that is the way it is handed to the various ethernet routines.  The format routine above will create a nnn.nnn.nnn.nnn type of string so you'll see or some such for addresses inside your router.  You can even compare it in the array form.  For example:

#define maskcmp(addr1, addr2, mask) \
(((((uint16_t *)addr1)[0] & ((uint16_t *)mask)[0]) == \
             (((uint16_t *)addr2)[0] & ((uint16_t *)mask)[0])) && \
            ((((uint16_t *)addr1)[1] & ((uint16_t *)mask)[1]) == \
             (((uint16_t *)addr2)[1] & ((uint16_t *)mask)[1])))

This mess of a macro (just copy and paste it, do not try to type that crap in) will compare the ip address (as an array) using the netmask to tell you if the machine that is coming in is inside your router or outside.  If you call it like this:


will return true if it is a local address.  Nice for protecting something from the outside.

DON'T GIVE UP !  You can do this and it will make a cool display that you can brag about for months.


i tried this code but the only ip it show's is allways :

where is the problem in the code?

byte client_ip[4];


    int c1=client_ip[1];
    int c2=client_ip[2];   
    int c3=client_ip[3];   
    int c4=client_ip[4];


Nov 27, 2011, 11:58 am Last Edit: Nov 27, 2011, 12:06 pm by SurferTim Reason: 1
Post the code from client.h and client.cpp for this function.
Code: [Select]

And this will only be valid while a client is connected.

Code: [Select]
Client client = server.available();


   // rest of your code


Has anyone managed to get this working with v1.0 of the IDE?
I tried changing the Client references to EthernetClient but cannot manage to get it to compile without errors.
I had hoped that remote IP would have been part of the library by now.
Any help would be appreciated.

Go Up