Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sending message to choosen client
QtWS25 Last Chance

Sending message to choosen client

Scheduled Pinned Locked Moved Unsolved General and Desktop
25 Posts 5 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    apoyo
    wrote on last edited by apoyo
    #1

    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
    8d2ec8de-489a-4c89-9710-d0f455b74453-image.png
    bd8efed1-e735-4a7e-b920-93f717cd0878-image.png
    Value DoKogo is server must equal value nazwa from client c9a0fc11-848b-4734-8cca-56031c3ed945-image.png
    I will be glad if someone can help me with this problem that has been bothering me for a long time

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      1
      • SGaistS SGaist

        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.

        A Offline
        A Offline
        apoyo
        wrote on last edited by
        #3

        @SGaist said in Sending message to choosen client:

        somehow

        but how to write it ? becouse i have no idea

        jsulmJ 1 Reply Last reply
        0
        • A apoyo

          @SGaist said in Sending message to choosen client:

          somehow

          but how to write it ? becouse i have no idea

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          1
          • jsulmJ jsulm

            @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.

            A Offline
            A Offline
            apoyo
            wrote on last edited by
            #5

            @jsulm i try by writing this in server but whether this map is visible by client?
            d42db547-60d8-4c6b-8397-2858d1c7e1d1-image.png

            And i have no idea how to write it correctly to work

            G JonBJ 2 Replies Last reply
            0
            • A apoyo

              @jsulm i try by writing this in server but whether this map is visible by client?
              d42db547-60d8-4c6b-8397-2858d1c7e1d1-image.png

              And i have no idea how to write it correctly to work

              G Offline
              G Offline
              giusdbg
              wrote on last edited by giusdbg
              #6

              @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.

              1 Reply Last reply
              0
              • A apoyo

                @jsulm i try by writing this in server but whether this map is visible by client?
                d42db547-60d8-4c6b-8397-2858d1c7e1d1-image.png

                And i have no idea how to write it correctly to work

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @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.

                A 1 Reply Last reply
                1
                • JonBJ JonB

                  @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.

                  A Offline
                  A Offline
                  apoyo
                  wrote on last edited by
                  #8

                  @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

                  JonBJ 1 Reply Last reply
                  0
                  • A apoyo

                    @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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @apoyo Good. Then follow what I wrote and implement it.

                    A 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @apoyo Good. Then follow what I wrote and implement it.

                      A Offline
                      A Offline
                      apoyo
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        apoyo
                        wrote on last edited by
                        #11

                        @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?
                        cd88fbd2-b090-4a50-b4f1-dcc45098a972-image.png

                        JonBJ 1 Reply Last reply
                        0
                        • A apoyo

                          @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?
                          cd88fbd2-b090-4a50-b4f1-dcc45098a972-image.png

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #12

                          @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?

                          A G JonBJ 3 Replies Last reply
                          0
                          • JonBJ JonB

                            @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?

                            A Offline
                            A Offline
                            apoyo
                            wrote on last edited by
                            #13

                            @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;
                            
                            }
                            
                            1 Reply Last reply
                            0
                            • JonBJ JonB

                              @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?

                              G Offline
                              G Offline
                              giusdbg
                              wrote on last edited by giusdbg
                              #14

                              @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

                              A 2 Replies Last reply
                              1
                              • G giusdbg

                                @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

                                A Offline
                                A Offline
                                apoyo
                                wrote on last edited by
                                #15

                                @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

                                G 1 Reply Last reply
                                0
                                • A apoyo

                                  @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

                                  G Offline
                                  G Offline
                                  giusdbg
                                  wrote on last edited by
                                  #16

                                  @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?'

                                  1 Reply Last reply
                                  0
                                  • G giusdbg

                                    @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

                                    A Offline
                                    A Offline
                                    apoyo
                                    wrote on last edited by
                                    #17

                                    @giusdbg
                                    i discover now that when i send a message from client and login into next client after that it disconnect :((

                                    1 Reply Last reply
                                    0
                                    • JonBJ JonB

                                      @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?

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by
                                      #18

                                      @apoyo

                                      @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?

                                      A 1 Reply Last reply
                                      0
                                      • JonBJ JonB

                                        @apoyo

                                        @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?

                                        A Offline
                                        A Offline
                                        apoyo
                                        wrote on last edited by apoyo
                                        #19

                                        @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);
                                        
                                        
                                        
                                        
                                        }
                                        
                                        
                                        G 1 Reply Last reply
                                        0
                                        • A apoyo

                                          @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);
                                          
                                          
                                          
                                          
                                          }
                                          
                                          
                                          G Offline
                                          G Offline
                                          giusdbg
                                          wrote on last edited by giusdbg
                                          #20

                                          @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.

                                          A 1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved