Sending message to choosen client
-
@giusdbg said in Sending message to choosen client:
@JonB @apoyo I don't know how to use sockets in QT.
But it seems to me that the problem is that the OP doesn't know how to accept multiple client connections on the same server.Maybe this can help
https://wiki.qt.io/WIP-How_to_create_a_simple_chat_application
maybe but if i send by button it works properlu@apoyo At work, we sometimes get scolded because a customer complains that one of our jobs doesn't work.
Sometimes someone replies that it works fine on our system.
The somewhat irritated response is 'so let's tell our customer to come work on your system?' -
@JonB @apoyo I don't know how to use sockets in QT.
But it seems to me that the problem is that the OP doesn't know how to accept multiple client connections on the same server.Maybe this can help
https://wiki.qt.io/WIP-How_to_create_a_simple_chat_application
-
@apoyo
Please do not post screen shots of code, nobody can interact with them. Use the forum's Code tags (</> icon) to paste your code.Do you think anybody can diagnose an issue from the code snippet you show here? How does it relate to multiple client connections?
Is this client-side or server-side code? It has
ui
in it so I guess client-side? If you have problems with multiple clients connecting or disconnecting then does it not seem likely that the issue is in the server code rather than client code? -
@JonB said in Sending message to choosen client:
If you have problems with multiple clients connecting or disconnecting then does it not seem likely that the issue is in the server code rather than client code?
@JonB maybe
Does somebody after viewing this code can tell why it is disconnecting second client when he login when the first client have already sended message?
m_server = new QTcpServer(); connect(this, &maineq::newMessage, this, &maineq::displayMessage); if(m_server->listen(QHostAddress::Any, 8080)) { connect(m_server, &QTcpServer::newConnection, this, &maineq::newConnection); //cout<<"Serwer nasłuchuje"; } else { // QMessageBox::critical(this,"QTCPServer",QString("Unable to start the server: %1.").arg(m_server->errorString())); exit(EXIT_FAILURE); } foreach (QTcpSocket* socket, connection_set) { socket->close(); socket->deleteLater(); } DB = QSqlDatabase::addDatabase("QSQLITE"); DB.setDatabaseName("C:/Users/vkocz/OneDrive/Dokumenty/DB/logowanie.db"); if(DB.open()){ // QMessageBox::information(this,"Connection","Database Connected Successfully"); } else{ //QMessageBox::information(this,"Not Connected","Database Connected UnSuccessfully"); } // m_server->close(); // m_server->deleteLater(); } /* maineq::~maineq(){ }*/ void maineq::newConnection(){ while (m_server->hasPendingConnections()) appendToSocketList(m_server->nextPendingConnection()); } void maineq::appendToSocketList(QTcpSocket* socket){ connection_set.insert(socket); connect(socket, &QTcpSocket::readyRead, this, &maineq::readSocket); connect(socket, &QTcpSocket::disconnected, this, &maineq::discardSocket); connect(socket, &QAbstractSocket::errorOccurred, this, &maineq::displayError); }
-
@JonB maybe
Does somebody after viewing this code can tell why it is disconnecting second client when he login when the first client have already sended message?
m_server = new QTcpServer(); connect(this, &maineq::newMessage, this, &maineq::displayMessage); if(m_server->listen(QHostAddress::Any, 8080)) { connect(m_server, &QTcpServer::newConnection, this, &maineq::newConnection); //cout<<"Serwer nasłuchuje"; } else { // QMessageBox::critical(this,"QTCPServer",QString("Unable to start the server: %1.").arg(m_server->errorString())); exit(EXIT_FAILURE); } foreach (QTcpSocket* socket, connection_set) { socket->close(); socket->deleteLater(); } DB = QSqlDatabase::addDatabase("QSQLITE"); DB.setDatabaseName("C:/Users/vkocz/OneDrive/Dokumenty/DB/logowanie.db"); if(DB.open()){ // QMessageBox::information(this,"Connection","Database Connected Successfully"); } else{ //QMessageBox::information(this,"Not Connected","Database Connected UnSuccessfully"); } // m_server->close(); // m_server->deleteLater(); } /* maineq::~maineq(){ }*/ void maineq::newConnection(){ while (m_server->hasPendingConnections()) appendToSocketList(m_server->nextPendingConnection()); } void maineq::appendToSocketList(QTcpSocket* socket){ connection_set.insert(socket); connect(socket, &QTcpSocket::readyRead, this, &maineq::readSocket); connect(socket, &QTcpSocket::disconnected, this, &maineq::discardSocket); connect(socket, &QAbstractSocket::errorOccurred, this, &maineq::displayError); }
-
@apoyo said in Sending message to choosen client:
Is there a reason for this?
foreach (QTcpSocket* socket, connection_set) { socket->close(); socket->deleteLater(); }
For the rest, I recommend looking at the example provided.
-
@apoyo Yes, but not being able to have an overview, I prefer to be perhaps erroneously suspicious.
Anyway, for example, you could put various 'connect' to disconnect, error, and similar events on sockets/descriptors and use QDebug to print when they are called.
If you have trouble viewing messages on a console, you can write to a log file (date hour minutes seconds text).In desperate cases I've used wireshark to trace connections on the network with step by step debugging, but it's like shooting with 120 howitzer at a fly.
The trace of the events is better.P.S. This is the client connection list that you will then need to process to send the same message to multiple clients,
connection_set
-
@apoyo Yes, but not being able to have an overview, I prefer to be perhaps erroneously suspicious.
Anyway, for example, you could put various 'connect' to disconnect, error, and similar events on sockets/descriptors and use QDebug to print when they are called.
If you have trouble viewing messages on a console, you can write to a log file (date hour minutes seconds text).In desperate cases I've used wireshark to trace connections on the network with step by step debugging, but it's like shooting with 120 howitzer at a fly.
The trace of the events is better.P.S. This is the client connection list that you will then need to process to send the same message to multiple clients,
connection_set
-
@apoyo If you're saying that debugging something works, but running the program usually causes the problem to return, it's often a sign that signal handling is incorrect or partial, or there are problems with object destruction (and using deleteLater() might stabilize the functioning).
The suggestion to look at the suggested example was also because it looks well set up and complete.