Pages: [1]   Go Down
Author Topic: Getting Client IP Address  (Read 840 times)
0 Members and 1 Guest are viewing this topic.
Memphis, TN
Offline Offline
Newbie
*
Karma: 0
Posts: 10
I <3 code.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys!

I've been trying to figure out a way to get the IP of a client when using the Ethernet library, I've been looking through w1500.h and w1500.cpp and have seen some stuff that might be used to accomplish this, although I can't seem to find where a lot of the functions are declared (such as writeGAR).

Can anyone point me in the right direction?

Thanks,
Aaron
Logged


- Aaron

Memphis, TN
Offline Offline
Newbie
*
Karma: 0
Posts: 10
I <3 code.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, I just stumbled onto this.  I took a wild guess and it looks like it may work:

Code:
void W5100Class::getRemoteIPAddress(SOCKET s, uint8_t *_addr) {
  readSnDIPR(s, _addr);
}

Update: For those interested, you can find my modifications here: https://github.com/ablakely/Ethernet-ablakely  smiley-grin
« Last Edit: March 24, 2012, 01:41:35 am by Dark_Aaron » Logged


- Aaron

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8966
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Some test code that has a remote IP function in it.

Code:
//zoomkat 12-8-11
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

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

byte rip[4];
//byte rip[] = {0,0,0,0};
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString;

//////////////////////

void setup(){

  pinMode(4, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print
  Serial.begin(9600);
  Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging
client.getRemoteIP(rip);
for (int bcount= 0; bcount < 4; bcount++)
     {
        Serial.print(rip[bcount], DEC);
        if (bcount<3) Serial.print(".");
     }

Serial.println();
          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Zoomkat");
               client.println();
               client.println(); 
             }
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
         
          client.println("<a href=\"/?on\" target=\"inlineframe\">ON</a>");
          client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>");

          //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
          client.println("<IFRAME name=inlineframe style=\"display:none\" >");         
          client.println("</IFRAME>");

          client.println("</BODY>");
          client.println("</HTML>");
             }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Memphis, TN
Offline Offline
Newbie
*
Karma: 0
Posts: 10
I <3 code.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you're talking about my library modifications, this:

Code:
client.getRemoteIP(rip);
for (int bcount= 0; bcount < 4; bcount++)
     {
        Serial.print(rip[bcount], DEC);
        if (bcount<3) Serial.print(".");
     }
Serial.println();

should be:

Code:
uint8_t socket = client.getSocket();
byte *rip[4];

client.getRemoteIP(socket, rip);
for (int bcount = 0; bcount < 4; bcount++)
{
    Serial.print(rip[bcount], DEC);
    Serial.print('.');
}
Serial.println()

Although, I might be able to make it work like the code you posted.
« Last Edit: March 24, 2012, 03:48:27 pm by Dark_Aaron » Logged


- Aaron

Memphis, TN
Offline Offline
Newbie
*
Karma: 0
Posts: 10
I <3 code.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I added some code so that:
Code:
client.getRemoteIP(rip)
and
Code:
client.getRemoteIP(socket, rip)
are both valid.
Logged


- Aaron

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8966
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Although, I might be able to make it work like the code you posted.

Although I haven't tested my posted code to any extent, it seems to work without a library modification (with the possible exception of the below mod I have done).

 http://code.google.com/p/arduino/issues/detail?id=605
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Memphis, TN
Offline Offline
Newbie
*
Karma: 0
Posts: 10
I <3 code.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well yesterday I spent 2 hours looking through the code before trying to make my own and found nothing.  I downloaded the current IDE and libraries yesterday morning then had to use the w5100.cpp and w5100.h off the github because the one I got from the arduino site would hang on Ethernet.begin().  But anyways I think I have the recent code and what I could tell there was no functions to do what I wanted so I wrote my own.
Logged


- Aaron

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I spend a lot of time to find a solution of error
Code:
no matching function for call to 'EthernetClient::getRemoteIP(byte* [4])'

The right way is to replace the line of code
Code:
byte *rip[4];
with
Code:
uint8_t rip[4];
or  uint8_t rip[] = {0,0,0,0};

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
Code:
uint8_t *EthernetClient::getRemoteIP(uint8_t *remoteIP)
« Last Edit: September 13, 2013, 12:28:08 am by grossman » Logged

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8966
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post that might be of interest.

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

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: