Go Down

Topic: Getting remote IP from ASocket (Read 1 time) previous topic - next topic

freelancer

Oct 08, 2011, 12:30 am Last Edit: Oct 08, 2011, 12:36 am by freelancer Reason: 1
I very recently got my Ethernet-enabled Arduino and I'm trying to get into network programming. I decided to use the Ethernet library from http://code.google.com/p/kegger/ (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1239258729/45) because it seems to have more features than the standard library, and I got it working more easily.
There's only one issue... Once I've opened a listening TCP socket with socket.initTCP(port) and socket.listenTCP(), and have a client connected, how do I get its IP? I've tried to find anything relevant in the code, but it doesn't seem to be there. This might be a really basic question, but I've never done any TCP/IP programming this low-level before.

Thanks!

SurferTim

#1
Oct 08, 2011, 12:49 pm Last Edit: Oct 08, 2011, 01:15 pm by SurferTim Reason: 1
With either library, modification of the ethernet library runtime code is required. Neither have a user function to retrieve the client IP, even tho the register function to read it is already in the library code.

I could help you get that client IP from the Arduino ethernet library, but not the library you are using.

Edit: I had to test it. My test code works fine with the Arduino ethernet library.
How about this for a function call:
Code: [Select]
byte ipBuffer[4];

client.remoteIP(ipBuffer)

freelancer

Did you write that function yourself? I can't find it in the original library code. But yeah, if you can show me how to get the IP with that library, I could probably adapt it to the one I'm using. Any help is appreciated.

SurferTim

#3
Oct 08, 2011, 02:39 pm Last Edit: Oct 08, 2011, 04:27 pm by SurferTim Reason: 1
Quote
Did you write that function yourself?

Yes. It didn't take much with the Arduino library. I am very familiar with it now.

Here are the additions:
In /libraries/Ethernet/client.cpp, add this. I put it after the Client::available() function.
Code: [Select]
uint8_t Client::remoteIP(uint8_t *buf) {
 if (_sock != MAX_SOCK_NUM)
   return W5100.readSnDIPR(_sock,buf);
 return 0;
}


In /libraries/Ethernet/client.h, add this. I again put it after the declaration for available().
Code: [Select]
 virtual uint8_t remoteIP(uint8_t *buf);

Then use it like this after the client.connect() or client.connected():

Code: [Select]
byte ipBuffer[4];
char outBuffer[18];

client.remoteIP(ipBuffer);
sprintf(outBuffer,"%u.%u.%u.%u\r\n",ipBuffer[0],ipBuffer[1],ipBuffer[2],ipBuffer[3]);
 

freelancer

Thank you! Changed it slightly to work with the ASocket class, and it works like a charm.

For reference:
Code: [Select]

bool ASocket::remoteIP(uint8_t *buf) {
if (_sock != MAX_SOCK_NUM) {
getSn_DIPR(_sock, buf);
return true;
}
return false;
}

SurferTim

You are welcome. Very good-looking function there! I can see the similarity of the two libraries now.

nathanas

#6
Jan 05, 2013, 01:57 am Last Edit: Jan 05, 2013, 01:59 am by nathanas Reason: 1
Dear surfer tim,
After having a new installation of windows on my PC, I tried to make tha library working again but with no luck.

The log is this one, and I am using Arduino 1.0.3.

Code: [Select]


homepage.ino: In function 'void homepage()':
homepage:30: error: no matching function for call to 'EthernetClient::remoteIP()'
C:\Program Files (x86)\arduino-1.0.3\libraries\Ethernet/EthernetClient.h:20: note: candidates are: virtual uint8_t EthernetClient::remoteIP(uint8_t*)
login.ino: In function 'void login()':
login:32: error: no matching function for call to 'EthernetClient::remoteIP()'
C:\Program Files (x86)\arduino-1.0.3\libraries\Ethernet/EthernetClient.h:20: note: candidates are: virtual uint8_t EthernetClient::remoteIP(uint8_t*)

PaulS

Compiler errors without code are pretty useless. All we can suggest is that you fix the problem(s).

nathanas

#8
Jan 05, 2013, 02:14 am Last Edit: Jan 05, 2013, 02:16 am by nathanas Reason: 1
The code had been working before with no problem.
I just wanted to upload the same code and I can;t because of the library. I am pasting the code given above to the EthernetClient.cpp & ethernetclient.h files with no syntax errors. Then I save the the notepad file as .cpp/.h

Code: [Select]
#include <SPI.h>        
#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
 0x00, 0x00, 0xAA, 0xBB, 0xCC, 0xDD };
IPAddress ip(192, 168, 10, XXX);

unsigned int localPort = 8070;      // local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP

EthernetServer server(8070);
void setup() {
 Serial.begin(9600);
 // start the Ethernet and UDP:
 Ethernet.begin(mac,ip);

  server.begin();
}

void loop() {

EthernetClient client = server.available();
 Serial.println("Waiting for client... ");
if(client) {
 Serial.println("Client request!!!! ");
while (client.connected()) {
 while(client.available()) {
  char c = client.read();
   if (c == '\n' ) {
      client.stop();
   }
   Serial.print("Received packet ...    ");

   Serial.print("From IP : ");

IPAddress clientIP = client.remoteIP();
   //print out the remote connection's IP address
   Serial.println(clientIP);


 
 }
 }
}
}



I think that when I managed to make the library work some time ago, I edited Ethernet.h/cpp & EthernetClient files. I am not sure. I had found a link from Surfer Tim and I don;t remember if its the same.

PaulS

Code: [Select]
IPAddress clientIP = client.remoteIP();
The remoteIP() method does not return an IPAddress. It takes an argument of a pointer to an IPAddress where the value is to be stored.

zoomkat

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

Go Up