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

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