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