how to detect when a server is disconnected
-
I have an client and server application in which i am able to transmit data and receive data and i can disconnect from server from client side but now my problem is.
In my server application i have a button that stops the server . in client side i have done some thing like "connect(&sock, &QTcpSocket::disconnected,this,&MainWindow::targetConnectionStatus,Qt::UniqueConnection);" this is triggered when i close the whole server application by pressing the windows close button , but when i press the stop server button this (i mean in the client side "connect" ) is not triggered. Is there any mistake i am doing ?
-
@ManiRon said in how to detect when a server is disconnected:
In my server application i have a button that stops the server
How does it "stop" the server? What exactly are you doing? Do you close the sockets on server side?
-
-
What does "stops the server" mean if it's different from "close the whole server"?
-
If you find that "close the whole server" does work, you need to do whatever similar in your "stop" code, e.g. perhaps close or destroy the socket, I don't know what it is you are actually doing?
-
Frankly, I think you are lucky if client disconnect is even fired on, say, server closure. Might depend on whether client & server are on same machine, might depend on TCP stack, but it's common in TCP protocol that one side cannot recognise if the other side "goes away", that's the point of the protocol. Make sure server closes socket properly, I think then client sees that....
-
-
@JonB said in how to detect when a server is disconnected:
that
actually when i press the stop server button which i have placed in that i will close the server. But what happens when we close the whole application . because when i close the whole application using the windows close button the the connect call is triggered
-
This is how my application looks like . when i click start server button
"connect(server->tcpServer, &QTcpServer::newConnection, server, &ServerStuff::newConnection);" this i have given
inside newconnection()
QTcpSocket *clientSocket = tcpServer->nextPendingConnection();
connect(clientSocket, &QTcpSocket::disconnected, clientSocket, &QTcpSocket::deleteLater); connect(clientSocket, &QTcpSocket::readyRead, this, &ServerStuff::readClient); connect(clientSocket, &QTcpSocket::disconnected, this, &ServerStuff::gotDisconnection); clients << clientSocket;
these are the lines
smilarly when i press stop server button
"if(server->tcpServer->isListening())
{
disconnect(server->tcpServer, &QTcpServer::newConnection, server, &ServerStuff::newConnection);QList<QTcpSocket *> clients = server->getClients(); server->tcpServer->close(); ui->textEdit_log->append(tr("<b>Server stopped</b>, post is closed")); }"
this is what is the code
ServerStuff(QObject *pwgt);
QTcpServer *tcpServer;
QList<QTcpSocket *> getClients(); -
-
@jsulm yes i tried but still the same , No actually i have made a connect in client side when ever the server side disconnects it will trigger my function in which i will print disconnected . this is triggered when i close the whole application but when i press that stop server button with what you have said it didnt work.
if(server->tcpServer->isListening())
{
disconnect(server->tcpServer, &QTcpServer::newConnection, server, &ServerStuff::newConnection);QList<QTcpSocket *> clients = server->getClients(); for(int i = 0; i < clients.count(); i++) { //server->sendToClient(clients.at(i), "Connection closed");
// server->sendToClient(clients.at(i), "0");
}//server->tcpServer->close(); server->clientSocket->deleteLater(); //server->tcpServer->close(); ui->textEdit_log->append(tr("<b>Server stopped</b>, post is closed")); }
-
You should take a look at
-
QAbstractSocket::disconnectFromHost()
At last someone points to a useful function! So does this mean if closing at server side it is its job to iterate each client and issue that call? There is not an automatic "shut me down and I'll disconnect from each client for you"? Just trying to understand.
-
@ManiRon
if I get this, right, than pressing stop server, behaves as expected but closing the window and therefore closing/quitting your app does not follow the correct server shotdown procedure?In that case I would suggest overriding the close event something along this line:
//My assumption is your Window class is named MainWindow, if not you'll have to adapt that //inside the header protected: virtual void closeEvent (QCloseEvent *event) override; //inside your cpp file #include <QCloseEvent> void MainWindow::closeEvent (QCloseEvent *event) { sameFunctionWhenStopServerClicked(); event->accept(); }
-
@J.Hilk
I'm afraid you're the wrong way round! For OP closing app does correctly disconnect, his problem is that he does not know exactly what code to write to make it work correctly if he does not close but wants his own code (off a button press) to do the socket closure such that client sees it.... For which @Christian-Ehrlicher has told him he will need to callQAbstractSocket::disconnectFromHost()
, and I am trying to verify with him this must be done explcitly on all connected client sockets.