Not able to connect arduino to localhost

Hello

I can connect reliably to Google either with www.GOOGLE.com through webclient example code in arduino IDE.
I can connect with my localhost ( Tomcat Apache) server from my browser (Chrome) by simply entering "http://localhost:8080" into the browser.
I can connect with my localhost using Chrome by its IP and port 192.168.0.5:8080.
I can also access JSP page on my localhost using Chrome.

But I cannot access my localhost web server with the arduino WebClient code

In the web client 'client.connect(server,8080) ; says connection failed.

Server address is same as obtain through ipconfig.

I have opened and enabled both port 8080 and port 80 in inbound and outbound rule of windows firewall.

I am able to access my server and jsp page from other computer connected on same network using http://localhost:8080/test.jsp and also checked the log file inside the tomcat apache in the server computer, the log is successfully registered.

But not able to access my web server from arduino uno.

Am i missing something in the settings etc to allow access of localhost web server from arduino ? Can anyone help pls ?

localhost means "this computer" i.e. the one you're running the request on. So using a browser on the web server you can use localhost to hit locally hosted pages.

I suspect that your observation that you can hit the web server from another computer using localhost is flawed.

Try connecting from your Arduino using IP and port.

Perhaps it is the lookup of the 'localhost' name that is the problem. Have you tried the localhost (a.k.a loopback) address? 127.0.0.1

wildbill:
localhost means "this computer" i.e. the one you're running the request on. So using a browser on the web server you can use localhost to hit locally hosted pages.

I suspect that your observation that you can hit the web server from another computer using localhost is flawed.

Try connecting from your Arduino using IP and port.

wildbill was right.. I tried connecting to web server (Tomcat Apache) from different computer using http://localhost:8080/test.jsp and it refused to connect. However i tried using http://192.168.0.5:8080/test.jsp from different computer and it successfully loads my test.jsp page. Please note 192.168.0.5 is the ip address of the machine running web server obtained by ipconfig.

I don't know how it got connected before using localhost, as per definition it should not. Anyway it is refusing connection now.

johnwasser:
Perhaps it is the lookup of the 'localhost' name that is the problem. Have you tried the localhost (a.k.a loopback) address? 127.0.0.1

From the computer running web server i tried following:

  1. http://127.0.0.1:8080/test.jsp --> page loaded successfully
  2. http://192.168.0.5:8080/test.jsp --> page loaded successfully
  3. http://localhost:8080/test.jsp --> page loaded successfully

Hardware connection wise, i am powering arduino from the computer which is running web server.

DHCP is able to allocate ip address to ethernet shield successfully using Ethernet.begin(mac)

Do i need to try anything else to find the exact cause, why arduino is not able to connect to webserver ?
Do i need to change any configuration at the router end ?

Guidance will be highly appreciated..!!

What do you see in your web server logs?

Can you try a basic HTML page instead?

I tried to load one more jsp page named iot1.jsp from my app2 folder,

As you suggested, i accessed index.html in app1 folder from remote computer.

Please find the consolidated log attached for both .jsp and .html page your reference.

I have also attached Arduino code in case you need.

Thank you.

localhost_access_log.2020-07-08.txt (3.35 KB)

iot1.ino (2.31 KB)

The web server seems to be reporting 200 which suggests it thought everything went ok.

Your Arduino code though closes the connection (stop) and then tries to read from it. That doesn't seem right.

I have commented cliente.stop() used after println();

Still it didn’t connect.

Its strange, i am able to connect my web server and load .jsp page successfully from remote desktop and even my mobile phone in same network but unable to connect from arduino.

I tried webclient example arduino code which access goggle.com via port 80 and arduino was able to connect and get response from google.com as i can see from my serial monitor window.

Is there anything to do with port 80 and 8080. Does tomcat apache need any configuration changes to allow access of port 8080 via ethernet ?
Does router need any port forwarding? But as i understand port forwarding is only required if i want to access my server from any external network.

I am really clueless and stuck at this stage.
Pls help..!!

Your logs show that your sever thinks it sent responses to 192.168.0.8. If you can hit google ok, it suggests that your code is working.

Possible issues: maybe you have an IP conflict. Maybe your library doesn't handle ports other than 80 (although it obviously should).

Can you ping that IP address? Can you ping it when the Arduino is off? Can you give the Arduino a static IP?

You might try getting a copy of Wireshark and see what traffic is coming and going on your web server.

Why are you using 8080? Try configuring Apache to respond on 80 instead.

As long as your Arduino and web server are on the same network , there should be no need for port forwarding.

I was using port 8080 because it was default port of Tomcat apache. However, based on your suggestions (wildbill) i have changed default port to port 80 and arduino got connected to tomcat which got reflected in log too. The only thing i got is server error 400 (bad request). After digging little further i got to know that HTTP 1.1 request should be Always followed by “HOST: Server ip” while it is not mandatory in case of HTTP 1.0 request.

Therefore, i added print(“Host:”); followed by println(server); in my previous code and it worked. The response i got is 200 as i can see in log and my database updated successfully.

Thanks everyone who guided me to move a step ahead. With your guidance (wildbill) i got to learn new things and my problem got rectified. Thanks a ton..!
I hope this discussion along with my last explanation may provide some help to others.