Get the IP address from computer name(hostname)

hi, I'm currently doing a project using Arduino [u]due[/u] with Ethernet shield. The arduino is the client and my PC is the server and i'm working on a local network.

Now everything is working as expected, but since the IP address on the server (my computer) changes from time to time i need to change it manually. I would like to have a more permanent solution for this.

I've tried using:

if(client.connect(serverIP, 80)) where serverIP is my computer-name (host), but it doesn't seems to work like it should. When serverIP is the server IP it works fine.

I've also tried using this: dns.getHostByName but again, something here is wrong.

any clues?

thanks, Yuval

Is the PC on the same localnet with a private IP, or is it on the internet with a public IP? If a public IP, is your PC registered with a dynamic DNS service?

Hey SurferTim, The server(PC) is on the same localnetwork as the client(arduino+sheild). The system is working just fine when i use the localIP of my pc.

My suggestion then is to set the PC IP as static, or set your localnet router to issue the same IP to that mac address every time it requests one (static dhcp assignment). Some routers can do that, and others can't.

Yes, this is also what i thought at first, but i was hoping to solve this issue in the program itself.

yuval_sh: Yes, this is also what i thought at first, but i was hoping to solve this issue in the program itself.

Unless you can resolve the hostname of the PC to an IP with DNS, you can't. Best way is to assign the server a static IP. Next best way is a static dhcp assignment.

edit: You could try running through all possible IPs except the Arduino's IP to see if you can find the server, but that may not work as well as you think if there are other server devices on the localnet.

Thanks SurferTim,
I think that i’ll try a bit more and if it won’t help i’ll go for your approach.

When i check the devices connected to my router, the arduino device name is listed as "WIZNETEFFEED". This name is not included in the server code loaded on the arduino, so it must be part of the w5100 chip programming.

Hey zoomket, Sorry for the late reply.

This is not exactly what i'm trying to do, I want to "tell" the Arduino due(as a client) what is the server IP (in a dynamic way) so it can communicate with it.

This is not exactly what i'm trying to do, I want to "tell" the Arduino due(as a client) what is the server IP (in a dynamic way) so it can communicate with it.

That is somewhat the reverse of the way things work. Servers usually don't initiate communication with clients.

This is exactly what i'm doing. The client initiate the communication.

I've tried to go deeper into the ethernet library. by looking at: EthernetClient::connect(const char* host, uint16_t port). I copied the function to the setup:

int ret = 0; DNSClient dns; IPAddress remote_addr;

dns.begin(Ethernet.dnsServerIP()); ret = dns.getHostByName("MyComputerName", remote_addr); if (ret == 1) { Serial.print("IP: "); Serial.println(remote_addr); } else { Serial.println("getHostByName Failed"); Serial.print("ret = "); Serial.println(ret); }

The value of "ret" is: ret = 65531. Any idea why?

Yes. The unsigned int value 65531 is the equivalent of -5 signed. That is the error return value from the dns search for "domain not found". Your dns server cannot resolve that domain name. I asked you about it in reply #1 and told you about it in reply #5.

Hello again Tim,
Indeed it is exactly what you said, but when i type: “MyComputerName” in the browser from any computer on my local network, i reach the server website, so i guess that it is working, or should work but i’m doing something wrong with the arduino programming.

I also tried instead of “mycomputername” to use:
http://mycomputername
“//mycomputername”

but no luck.

Your PC/web browser may be using WINS (Windows Internet Name Service) or LMHOSTS (Lan Manager Hosts) to locate your server. Your DNS search does not access WINS or LMHOSTS. A Google search on either of those terms may help you.

The best way to determine if your dns server is able to resolve the domain is an app called nslookup. It is available for Windows in the optional networking package.

I see...thanks again for your help. For now i'm using your original advice about static IP.

If I get time, I will research the WINS and LMHOSTS protocols. Maybe there is a way to access those protocols with the ethernet shield.

edit: AFAIK, those protocols are only good on the same localnet. If the server is on a different localnet, I don't think they will work.

Are you trying the http://mycomputername on the server with that host name assigned ? If so, it will respond to that host name.