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.
Related
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.
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 am creating a chat app using smack using openfire sever by XMPP protocol. I can chat with other user and working well. I created connection in Service class. So i am able to get notification message when i am not in my application but i get my service is restarting again and again when network problem, network change, network slow etc. So my connection is lost. and user is not always able to chat.
So i solved connection problem. like as
if( service is not runing)
start my service
else
if(connection is NULL or not Connected)
reconnection . so my connection is created
My service class:-
if(connection is NULL or not connected)
reconnect. so my connection is created
else
not connected.
But now if my service is runing and connection is lost. and i open my application , a new connection is created. This connection is created by application not services. So now when i am not in my app , i did not get any notification for incoming message because incoming message was coming by service connection.
Now i have involved. please expert help me. How can i solve?
Is there way for push notification in openfire?
You could try binding to your service;
Create a method in your service called connect()
When the application is started instead of creating a connection object in activity class, bind to the service, call myServcie.connect();
This way the connection is still owned by the service,
Other way to communicate with the service is via intents.... send an intent to the service when activity is created and have the BroadcastReceiver in the Service call connect();
One thing to watch out for is continuously reconnecting in a mobile environment is not recommended.... battery life being what it is. You can check to see if the application package is the one that is showing and then reconnect if it is etc.
James
I have created a chat application. My Service has a connection which I used in my application. when an incoming message arrives then it proves that Service class's connection with the server is alive. When my network connection has failed then the connection with the server is disconnected but the Service is still running.
Is there a way to ensure that my connection with the server will be alive for all time? If any network problem is found, how do I make sure it is automatically re-established within the Service.
Thank you.
I am working on sending mail from a broadcast receiver using javax.mail. The application sends mail when the app is running and in foreground. But when i move the application to background, I get an error
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
java.net.ConnectException: failed to connect to smtp.gmail.com/74.125.25.108 (port 587): connect failed: ENETUNREACH (Network is unreachable)
I have made sure that the broadcast receiver is running in background.
Just a little note, I have had trouble doing internet based services from Broadcastreceiver in the past. It would work one time, fail the next. This can be something simple like slow 3G data or a failed post. The trouble with Broadcastreceiver is it gets aggressively shut down by the GC in some cases and is not meant to be long-running. So for things that could fail, or take long, or need to be retried (like sending email), consider rather starting a service from your broadcastreceiver which can continue to retry and keepalive in the event of failure. Not essential, but a good idea (in my experience).