After launching the application, it connection to the server. I am using Socket. TCP connection type. As long as the application is active on the screen, everything is fine. But if the application is moved to the background and the screen turns off, and after a while the connection with the server is lost, then it is impossible to create a connection again. The socket throws an exception:
"Unable to resolve host "m15.cloudmqtt.com": No address associated with hostname".
Where "m15.cloudmqtt.com" is the server name.
But if you take the phone out of the background and open the application, then the connection occurs immediately.
The creation of a socket is implemented using AsyncTask, which is launched from the Service.
Is it possible to solve this problem? I have been looking for various solutions for several days.
Related
I have a web app that is now being used in an Android app by launching it in a webview. Most of the Android functionality is native & the webview part is mainly just for communication via socket.io
It all works okay but as soon as the phone goes to sleep it kills the socket connection after a few minutes. This connection is used for incoming messaging so I need the connection to stay alive.
I tried creating a foreground service and using it as a keep-alive but since the socket connection is in the webview, it still gets closed no matter what I do i.e. the service stays alive and I can make a new network connection, but I can't keep the existing app connection alive.
Is there a way to keep a webview socket connection alive when the phone dozes? Am I doing something wrong? Or is my design flawed and I need a different approach?
I'm working for an App Android and I need my phone was always connected to my server MQTT...
But my phone can lost his connection(Tel :[FIN;ACK]; Serv:[FIN;ACK];Tel:[ACK]); after that, when he send a [SYN] message for connect it to the server MQTT, the serveur send an [RST,ACK]... And this while the service of my app is in activity.
In my case , i MUST to re-launch my server if i want my phone can be reconnect, but, i can't reboot my server (multi-phone possible, and i need real-time performence).
There is there a possibility to connect my phone without reboot my server?
Thank's for time.
Best regard
Guillaume
I think your problem is similar to the one I had. The connection was half closed. See http://www.codeproject.com/Articles/37490/Detection-of-Half-Open-Dropped-TCP-IP-Socket-Conne for details.
The way I solved this was to i) add keep alive msgs, every 5 secs. from the phone to the server and ii) have the server close the connection if there was no incomming data for 15sec. See http://www.forward.com.au/pfod/CheapWifiShield/index.html and www.pfod.com.au
MQTT includes keepAlives (PING request msgs) so you only need to modify your client to specify a short keepAlive interval
see http://www.hivemq.com/blog/mqtt-essentials-part-10-alive-client-take-over
to have the MQTT server to more quickly close the connection once it stops getting your data or keepAlive msgs.
Although the above link states
"The broker will close the previous connection to the same client (determined by the same client identifier) and establishes the connection with the newly connected client. This behavior makes sure that half-open connection won’t stand in the way of a new connection establishment of the same client."
So you should not be having this problem at all !!!
I have developed an Android application that uses a TCP socket connection that listens on and writes to a particular port. My issue is, every time data connection switches to an EDGE connection, the application force closes. I have try catch blocks, none of which throw an exception. Is there a reason why this should happen?
Note: it works fine on other connection types such as HSDPA.
I have developed one application which talks to the server to send and receive data, for that we have implemented activities and one background service so each activity has to register with the service when it needs to send any request so service will further forward that request to the server via Wi-Fi router.we have established socket connection with the server and data flows through it.
Problem: when device goes to sleep mode and after some time if you turns on device my app is disconnected from the server and in logcat following exception comes:
SntpClient(789): request time failed: java.net.SocketTimeoutException: Try again
I would like to know why this exception comes and how to resolve it.
I'm creating an android app. It requires a TCP socket connection to send data to the another device.
Everything works fine for the first time transfer. However when I connect it the second time, it says connection refused by the host or bind error.
I did all possible things to solve this bind error and connection refused. I shutdown both reading and writing end once the transfer is complete, set socket option to re use address, lingering option.
These things work fine when I exit the application completely and open it again but it does not work when I try to open the connection without exiting.
Kindly, help me figure out whether the problem is something related with sockets or with the application, and any ideas how this could be solved?
Look at the output of netstat -a after you close the socket on the server side and before you retry anything. If your ipaddress/port pair is still in any state other than, I think TIME_WAIT, you will not be able to reconnect.
The server side (the one that calls accept) is most likely the culprit, but I'd check both now that you see something wrong.