Getting remote IP from ASocket

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 Google Code Archive - Long-term storage for Google Code Project Hosting. (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!

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:

byte ipBuffer[4];

client.remoteIP(ipBuffer)

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.

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.

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().

  virtual uint8_t remoteIP(uint8_t *buf);

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

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]);

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

For reference:

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

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

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.

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*)

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

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

#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.

 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.

Past discussions:

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