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.
Related
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'm developing a queue of messages for a messenger app.
On Android if the connection is lost, messages is save on the queue and as soon as the 3G or wifi is back the messages go out of the queue. This is possible thanks I'm listening a broadcast receiver which alert me when the connection is back.
Is there anything similar on iOS?
Any workaround on iOS to achieve this behaviour?
You determine the restoration of an internet connection via the Reachability class. You can attempt resending once Reachability tells you that you have an Internet connection again.
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 currently developping an Android application which connects to a server through TCP. The server is written in Qt and runs on a computer.
In server side, I use a QTcpServer and the signal QTcpServer::newConnection() to get the QTcpSocket newly connected with QTcpServer::nextPendingConnection(). I have implemented a class I called SocketManager, which manages the data received by this socket.
In Android side, I use java Socket to connect to the server.
All work great. When the Android side disconnects from the server, my SocketManager object is well notified and destroys itself. But I would like to manage properly the case when for example the Android device goes to offline or is turned off. In that case, I'm not notified of the disconnection of Android. I connect these signals of my QTcpSocket:
QAbstractSocket::disconnected(),
QAbstractSocket::stateChanged(QAbstractSocket::SocketState)
QAbstractSocket::error(QAbstractSocket::SocketError)
QObject::destroyed(QObject*), thinking that perhaps the QTcpSocket is internally destroyed by the QTcpServer.
But no signal is received when the Android device goes offline or is turned off.
When the QTcpSocket will be released by the QTcpServer? Only when the socket is explicitely disconnected? So in my case, will it never be destroyed? Should I manage the disconnection in all cases in the Android side?
Thanks everyone.
TCP will not notify you of disconnections unless the remote peer explicitly sends disconnect request (by using close() or shutdown() methods) or you try to write to a disconnected socket (in which case you get a broken pipe signal)
The classical way to solve this problem is implementing a heartbeat messaging system where after a certain amount of heartbeat inactivity you close the socket concluding that the remote peer has died suddenly or there is a network problem.