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
Forum Updated to NodeBB v4.3 + New Features

Sending message to choosen client

Scheduled Pinned Locked Moved Unsolved General and Desktop
25 Posts 5 Posters 2.3k Views 2 Watching
  • 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.
  • 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
                            • G giusdbg

                              @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 Offline
                              A Offline
                              apoyo
                              wrote on last edited by
                              #21

                              @giusdbg
                              if i comment this part of code it still disconnected,so it is not reason for this

                              G 1 Reply Last reply
                              0
                              • A apoyo

                                @giusdbg
                                if i comment this part of code it still disconnected,so it is not reason for this

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

                                @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

                                A 1 Reply Last reply
                                0
                                • G giusdbg

                                  @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

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

                                  @giusdbg now something works algothough i dont really why, i do debug and one line i delete

                                  G 2 Replies Last reply
                                  0
                                  • A apoyo

                                    @giusdbg now something works algothough i dont really why, i do debug and one line i delete

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

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

                                    1 Reply Last reply
                                    0
                                    • A apoyo

                                      @giusdbg now something works algothough i dont really why, i do debug and one line i delete

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

                                      @apoyo One addition, if it works, it may be useful to use a telnet client program to try to connect to the server and send commands to it.

                                      1 Reply Last reply
                                      1

                                      • Login

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