Pages: [1]   Go Down
Author Topic: Getting remote IP from ASocket  (Read 1672 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
« Last Edit: October 07, 2011, 05:36:52 pm by freelancer » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
byte ipBuffer[4];

client.remoteIP(ipBuffer)
« Last Edit: October 08, 2011, 06:15:32 am by SurferTim » Logged

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

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

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
 virtual uint8_t remoteIP(uint8_t *buf);

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

Code:
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]);
 
« Last Edit: October 08, 2011, 09:27:59 am by SurferTim » Logged

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

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

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

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 179
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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*)
« Last Edit: January 04, 2013, 07:59:17 pm by nathanas » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 179
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
« Last Edit: January 04, 2013, 08:16:19 pm by nathanas » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Past discussions:

http://arduino.cc/forum/index.php/topic,135082.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: