I tried connecting to an IP address (e.g. http://222.222.222.222:8080) and a URL (e.g. http://www.website.com) while my wiFi is disabled. I noticed that if I don't have a wiFi and I tried connecting to an IP address, it gives me a ConnectException error. On the other hand, if I don't have a wiFi and I tried connecting to a URL, it gives me an IOException error. Why am I receiving different Exception for the 2 cases when the only difference is I supplied an IP address for the first one and a URL for the other? Can someone enlighten me on this one? I am asking this for clarification.
Thank you!
You're getting different errors because different steps are failing.
When you're trying to connect to port 80 of an IP address, it is a connect(2) system call that is failing. There are many different reasons why connect(2) could fail; you'll need to inspect the message from the exception to provide a good error message to the user.
When you're trying to connect to port 80 of a textual address, the libraries will first try to resolve the hostname into an IP address using getaddrinfo(3). The name resolution may or may not fail based on having network access -- if you were trying to connect to localhost, for example, no network access is usually required, as the nameservice lookup can be handled entirely on the local device. Because the nameservice failure happens because you cannot contact a nameserver, it makes sense to give a different error message (and exception), even if the underlying cause is the same for a given set of tests. You might not be able to contact the nameservers for any variety of reasons. Again, you'll need to inspect the message from the exception to give a good error message to the user.
Related
In my Android app, I do poll a device (Wallbox) every 5 seconds using the Volley library. For one customer, I get
java.net.NoRouteToHostException: Address not available
after several hours. Usually, using the exactly same type of device and at my place, this polling works for days, but not at (at least) this customer.
The device address is on the local LAN.
I have not found any description about "address not available" that might apply to my case, or at least I did not see the point.
The call is always the same:
http://192.168.160.152/api/status
As this is live data, I set ".setShouldCache(false)" for the request.
Meanwhile, other HTTP requests (in this case, a query to a WWW address, also polled continuously), even using Volley, continue to work:
https://backend.powerfox.energy/api/2.0/my/***********/current
The connection works again after rebooting the device (and at the same original IP address), so I guess it has nothing to do with the wallbox or the router.
And if the wallbox would be not available, I assue I would get "unreachable" error or such.
Is there someone who can help me, what could cause that exception ("address not available"), and what can I do about it?
I built a flutter app which communicates with a web server that I wrote with flask. Everything works as intended without any errors if I use a virtual device. As soon as I try it in release mode on a physical device I get problems when it comes to the communication with the server
The only thing that I changed when using a physical device is the ip. I use 10.0.2.2 on the virtual device and my computers ip4 adress - that I get with ipconfig in windows 10 - on the physical device
Both devices are in the same network connected to the same router
Internet Permission is enabled in the AndroidManifest for all modes (Debug, Main, Profile)
I even disabled the firewall
The line that causes the issue is
await http.get(url).timeout(Duration(seconds: 15), onTimeout: () {
// Handle timeout
// This entire thing is in a try-catch block in an async function
});
In debug mode on the physical device when the HTTP get request is sent VSCode immediately says
Exception has occurred.
SocketException (SocketException: OS Error: Connection refused, errno = 111, address = 192.168.178.20, port = 43378)
First Question: Why Port 43378? Is that the port the HTTP request is sent to? Because when I run the flask app it says:
Running on http://127.0.0.1:5000/
Could that be the issue? I would have expected the exception to say the port is 5000 as declared in the URL. Or do I have to change something with how I set up the flask app? Currently it is the development server because I am still testing before I pay money and deploy
However I hope I didnt forget any important information. Any advice on what could be wrong or how to debug here is highly aprecciated
Pass an Uri object to http.get func. Uri classes let you specify the port as Documentation https://api.dart.dev/stable/2.12.0/dart-core/Uri-class.html
I have get UnknownhostException in parsing JSON data from server,
My URL working on :
http://jsonlint.com
on real device browser(data plan only)
Sometimes its working on PC browser and sometimes not.
Actually I have get the wifi issue, Sometimes its working on wifi connection, when its not, I have restarted my wifi and its worked, and after some time the same issue came,
I want to get the permanent solution for this wifi connection issue, Restarting the Wifi is never a solution on the application user side,
What is the exact problem and solution also...
Usually the UnknownHostException fires when you cannot resolve the DNS record of the URL you've provided. There's a reasonable timeout for that operation, but if you have a weak WiFi connection or you don't have enough signal on your device, the communication can be interrupted in the middle between sending and receiving the response, so your device doesn't receive the response, thus it thinks it's a DNS timeout.
There are 2 things you can try:
Increase the timeout of the response. This will not help, though, if your communication gets interrupted you already sent the query.
Use the IP address instead:
shut-up#i-kill-you:~$ ping jsonlint.com
PING jsonlint.com (54.243.171.164) 56(84) bytes of data.
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
int linkSpeed = wifiManager.getConnectionInfo().getRssi();
System.out.println("Link Speed is======"+linkSpeed);
It will give u wifi speed so just mention one speed if that speed come then only do next work
My app needs to contact the same device it is working on, via http://127.0.0.1/... (a localhost url).
For some reason, about 50% of the times (and maybe exactly 50%) when I reach a website there with JSON content, I get the exception:
java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
For the other 50%, I get perfectly good results. I've tried to do polls (and even large delay between polls), but I keep getting the same weird results.
I've searched the internet and also here, and I'm not sure why it occurs. Does the peer mean that the client has caused it? Why does it happen, and how should i handle it?
Some websites say that it's a common thing, but I didn't find what's the best thing to do in such cases.
Ok, the answer was that it's the server's fault - it had to close the connection after each request.
It might be that Android keeps a pool of connections and use the old one or something like that.
Anyway , now it works.
EDIT: according to the API of HttpURLConnection, this can be solved on the client side too:
The input and output streams returned by this class are not buffered.
Most callers should wrap the returned streams with BufferedInputStream
or BufferedOutputStream. Callers that do only bulk reads or writes may
omit buffering. When transferring large amounts of data to or from a
server, use streams to limit how much data is in memory at once.
Unless you need the entire body to be in memory at once, process it as
a stream (rather than storing the complete body as a single byte array
or string).
To reduce latency, this class may reuse the same underlying Socket for
multiple request/response pairs. As a result, HTTP connections may be
held open longer than necessary. Calls to disconnect() may return the
socket to a pool of connected sockets. This behavior can be disabled
by setting the http.keepAlive system property to false before issuing
any HTTP requests. The http.maxConnections property may be used to
control how many idle connections to each server will be held.
Taken from:
developer.android.com/reference/java/net/HttpURLConnection.html
Try to set this property for your HttpURLConnection before connecting:
conn.setRequestProperty("connection", "close");
This will disable "keep-alive" property which is on by default.
This is an old thread i know. But this might help someone.
In my case this error was caused by the .NET WCF (soap) service. One of the objects in the returning result had a DataMember with get{} property but no set{} property.
For serialization to occur every DataMember should have both get{} & set{} available. I implemented an empty set{} (empty due to my business rules), and problem was solved.
My scenerio is a specific bad server implementation, but maybe it'll help someone saving time when troubleshooting.
I was having a lot of these Connection reset by peer when I was visiting certain web pages or downloading files (from my app or the Android browser).
Turned out it was my 3G carrier that blocked the connections (e.g. downloading an .exe file was forbidden).
Do you have the same problem on Wifi ?
in my situation the problem has been solved by cleaning the proxy address and port from APN which was produced by the operator.
as I have tested, using IP address of remote server instead of domain name also can solve the problem.
In our case the issue is in the Server side (Application Pool configuration in IIS). I resolved it by setting Maximum Worker Processes to 1. Setting it with value more than 1 will enable Web Garden which seems to be the issue.
Well, tried the following bit of code:
import jcifs.netbios.NbtAddress;
...
try{
NbtAddress addr = NbtAddress.getByName("Base");
}catch (UnknownHostException e){}
...
And it throws that exception.
Any ideas? I have tried it with a couple of named machines on the LAN
I guess you don't have the INTERNET permission in your manifest?
I was using the emulator.
When I ran the app on real hardware the problem disappeared.
The problem is that NBT name resolution works by sending UDP broadcast packets on the LAN, and the emulator lives on its own virtual LAN, connected to your host PC only by a virtual NAT router. Broadcast packets don't cross between different subnets, so nothing on your LAN sees the name query.
I have the same problem. The only ideas for solving it that I know are:
Set up a WINS server on your LAN, and point your app at it with the jcifs.netbios.wins property,
or,
Create an LMHOSTS file in the emulator's filesystem, containing the names and IPs of the hosts on your LAN that you want to be able to reach, and point your app at it with the jcifs.netbios.lmhosts property.
If it was possible to run something on the emulator that could serve as a NetBIOS Master Browser, there might be a third solution, but I haven't researched that enough yet.
The LMHOSTS solution isn't ideal, because you don't (AFAIK) get to browse for workgroups and hosts. That should work with a WINS server, but I haven't tried it.