Sending message to choosen client
-
I am writing an chat application which uses client server architecture.I have problem how to access one variable in client from server.I would like to server sending QString only to clients who have username chosen in comboBox in client.I am using TcpSocket in this project. How to connect this values from client by server to return this to client which have selected username.In server i have values from the user who send the message and to whom it have to be sended ??
From is called OdKogo
To Whom is called DoKogo
Value DoKogo is server must equal value nazwa from client
I will be glad if someone can help me with this problem that has been bothering me for a long time -
Hi,
If you want to be able to send direct messages you have to somehow keep a map between the connected user and its connection/socket.
One really basic way is to use a hash that uses the user name as key and the corresponding socket as value.
-
Hi,
If you want to be able to send direct messages you have to somehow keep a map between the connected user and its connection/socket.
One really basic way is to use a hash that uses the user name as key and the corresponding socket as value.
-
@SGaist said in Sending message to choosen client:
somehow
but how to write it ? becouse i have no idea
@apoyo said in Sending message to choosen client:
but how to write it ?
@SGaist already suggested how: "One really basic way is to use a hash that uses the user name as key and the corresponding socket as value.".
Use QHash or QMap with user name as key and pointer to socket as value. -
@apoyo said in Sending message to choosen client:
but how to write it ?
@SGaist already suggested how: "One really basic way is to use a hash that uses the user name as key and the corresponding socket as value.".
Use QHash or QMap with user name as key and pointer to socket as value. -
@jsulm i try by writing this in server but whether this map is visible by client?
And i have no idea how to write it correctly to work
@apoyo There can be no sharing of memory/object between programs residing on two different systems.
I can't give you any code, but usually there are this ways
1
Each client talks to its own execution of the server program, if there is a need to send messages to multiple clients it is done using something common to the various server programs (only for example, database table).2
When the server opens a new connection with a new client, it has an object available, an instance that refers to this precise new connection.
This object/instance must be stored and associated with a text or other that uniquely identifies the client (usually sent during connection by the client or the server itself).
Then you need to replace the foreach on the connection set with a foreach on the set of objects that contain the association to the connection and the unique reference.
To these objects you add all the data that is necessary to perform the operations that the server must perform.In summary, the client will then send a message to the server, I'm client IDxxx, my new username is USRyyyy, and the server will set the data in the related association object.
.
P.S. In reality, between 1 and 2 the only difference is how and where, the univocal associations between client and server and the information necessary for the server to perform the desired operations, are stored. -
@jsulm i try by writing this in server but whether this map is visible by client?
And i have no idea how to write it correctly to work
@apoyo said in Sending message to choosen client:
@jsulm i try by writing this in server but whether this map is visible by client?
Of course not! Not a single variable in a client or server program can possibly be visible to the other side, unless you exchange a copy of it first using whatever protocol they agree on.
It's hard to be sure what situation you describe. I think you have a single server application and multiple client applications? Every message from a client first goes to server, and then server forwards it to all other clients? Clients do not speak directly to other clients?
So for a client to request a message be sent only to one other client, invent a protocol where the client can tell the server "the following message is only to be sent to this username". Have the server maintain a list of clients/sockets by username (e.g. in a
QHash
like others have said).If the clients want a list of the available usernames (e.g. to display in a combobox) then invent a protocol whereby the client can request a list of connected usernames from the server.
-
@apoyo said in Sending message to choosen client:
@jsulm i try by writing this in server but whether this map is visible by client?
Of course not! Not a single variable in a client or server program can possibly be visible to the other side, unless you exchange a copy of it first using whatever protocol they agree on.
It's hard to be sure what situation you describe. I think you have a single server application and multiple client applications? Every message from a client first goes to server, and then server forwards it to all other clients? Clients do not speak directly to other clients?
So for a client to request a message be sent only to one other client, invent a protocol where the client can tell the server "the following message is only to be sent to this username". Have the server maintain a list of clients/sockets by username (e.g. in a
QHash
like others have said).If the clients want a list of the available usernames (e.g. to display in a combobox) then invent a protocol whereby the client can request a list of connected usernames from the server.
-
@JonB Yes i have one server and many clients.clients send message by clickbutton and message goes to the server and server forwards it to client
-
i manage to do this according to @giusdbg idea,the socket->socketdescriptor in the server is write in ReadSocket function,but i have one problem ,that there is one requirements i want to abolish ; there is neccessity of sending the message by clicking button to be possible sending to this client
-
@apoyo now i do this by this code but there is a problem that if i connect second client it disconnect ,without this code it work and it not disconnect ,but i need a possiblity of sending messages to chosen client without firstly send this message from chosen client,how to do this?
-
@apoyo now i do this by this code but there is a problem that if i connect second client it disconnect ,without this code it work and it not disconnect ,but i need a possiblity of sending messages to chosen client without firstly send this message from chosen client,how to do this?
@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? -
@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 it is code in client side .If i pass this code into button it work and it not disconnect as i check minute ago
void messageprep::on_pushButton_3_clicked() { QString strd="Welcome to the server"; QString DoKogo; QString OdKogo; QDataStream welcome(socket); DoKogo=nazwa; OdKogo=nazwa; welcome<<strd<<DoKogo<<OdKogo; }
-
@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 @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
-
@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
@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 -
@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); }