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. cannot write through opened socket

cannot write through opened socket

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 2 Posters 941 Views 1 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by user4592357
    #1

    i open a tcp socket with startCommunication() function, which succeeds.

    class SocketDescriptor : public QTcpServer
    {
    	Q_OBJECT
    public:
    	explicit SocketDescriptor(bool bServer = true, QObject *parent = 0)
            : QTcpServer(parent),
            m_bServer(bServer),
            port(0),
        {}
    
    	bool startCommunication(QString strHostName, int nPort)
        {
            port = nPort;
            QHostInfo hostInfo = QHostInfo::fromName(strHost);
    
            addr = !hostInfo.addresses().isEmpty()
                ? hostInfo.addresses().front() : QHostAddress::Any;
    
            bool m_bStarted = listen(addr, port);
            if (!m_bStarted)
            {
                setError(QString("Server cannot be started on port %1").arg(port));
                return false;
            }
            else
            {
                addr = this->serverAddress();
                port = this->serverPort();
            }
    
            return m_bStarted;
        }
    
    	bool isConnectionAvailable() const
        {
            QTcpSocket* pSocket =  getSocket();
            return (pSocket && pSocket->isOpen());
        }
    
    public:
    	bool m_bServer;
    	bool m_bStarted;
    	QHostAddress addr;
    	int port;
    
    protected:
    	QTcpSocket *m_ClientSocket{ nullptr };
    	QString m_LastError;
    	QThread *m_pCommunicationThread{ nullptr };
    };
    

    this operation succeeds, then i start another application with it which expects read and write ports:

    port = // get the port number
    
    const QString sPort(Port);
    auto args = QStringList() << sPort << sPort;
    
    if (!QProcess::startDetached(sClientName, args))
    {
        const QString sProgFullPath{ QString::fromLatin1(pDir) +
            QLatin1String("/bin/") + sClientName };
        if (!QProcess::startDetached(sProgFullPath, args))
            return false;
    }
    

    this succeeds, i don't get any error.
    but when i send this other application a message through the socket port stored before, the isConnectionAvailable() returns false, and i cannot send the message.

    if (!isConnectionAvailable())
          return;
    if (m_Socket->write(MsgBytes) != -1)
          m_Socket->flush();
    

    i want to know what i'm doing wrong.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Please take a look at the QTcpServer example. Your code does ... nothing really useful.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      U 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        Please take a look at the QTcpServer example. Your code does ... nothing really useful.

        U Offline
        U Offline
        user4592357
        wrote on last edited by user4592357
        #3

        @Christian-Ehrlicher my code is this

        open socket with above code, it returns port number
        port = // get the port number
        
        const QString sPort(Port);
        auto args = QStringList() << sPort << sPort;
        
        if (!QProcess::startDetached(sClientName, args))
        {
            const QString sProgFullPath{ QString::fromLatin1(pDir) +
                QLatin1String("/bin/") + sClientName };
            if (!QProcess::startDetached(sProgFullPath, args))
                return false;
        }
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          This code can not work. Follow the QTcpServer examples.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          U 2 Replies Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            This code can not work. Follow the QTcpServer examples.

            U Offline
            U Offline
            user4592357
            wrote on last edited by user4592357
            #5

            @Christian-Ehrlicher this is working code.
            i just tried this
            i created socket with above method, and run telnet localhost $port
            then i sent message to telnet using this port, and it was sent...
            so... is that other app faulty?

            1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              This code can not work. Follow the QTcpServer examples.

              U Offline
              U Offline
              user4592357
              wrote on last edited by
              #6

              @Christian-Ehrlicher my bad
              the other app expects file descriptors, and i'm providing socket port
              so, how should i work with that app?

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @user4592357 said in cannot write through opened socket:

                so, how should i work with that app?

                It's your app - how should we know?

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                U 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @user4592357 said in cannot write through opened socket:

                  so, how should i work with that app?

                  It's your app - how should we know?

                  U Offline
                  U Offline
                  user4592357
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher i mean it is accepting file descriptors, and i have socket (port).

                  how should i convert socket to fd?
                  can i do that?

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @user4592357 said in cannot write through opened socket:

                    how should i convert socket to fd?

                    By taking a look at the documentation: https://doc.qt.io/qt-5/qabstractsocket.html#socketDescriptor

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    U 1 Reply Last reply
                    1
                    • Christian EhrlicherC Christian Ehrlicher

                      @user4592357 said in cannot write through opened socket:

                      how should i convert socket to fd?

                      By taking a look at the documentation: https://doc.qt.io/qt-5/qabstractsocket.html#socketDescriptor

                      U Offline
                      U Offline
                      user4592357
                      wrote on last edited by
                      #10

                      @Christian-Ehrlicher thanks for the link

                      so when i create the child app like this, when main app is closed, child app still exists, as it was started detached:

                      if (!QProcess::startDetached(sClientName, args))
                      {
                          const QString sProgFullPath{ QString::fromLatin1(pDir) +
                              QLatin1String("/bin/") + sClientName };
                          if (!QProcess::startDetached(sProgFullPath, args))
                              return false;
                      }
                      

                      i tried to convert this to using start() instead, but looks it's not equivalent (because now error signal is async?)
                      what's the correct way to nest such call?

                      QProcess p1;
                      p1.start(sClientName, args);
                      connect(&p1, &QProcess::errorOccurred, []
                      {
                          const QString sProgFullPath{ QString::fromLatin1(pDir) +
                              QLatin1String("/bin/") + sClientName };
                      
                           QProcess p2;
                           p2.start(sProgFullPath, args);
                           connect(&p2, &QProcess::errorOccurred, []
                           {
                                // how do i return false from here?
                          });
                      }
                      
                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Start both directly instead waiting for the first one.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        U 2 Replies Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          Start both directly instead waiting for the first one.

                          U Offline
                          U Offline
                          user4592357
                          wrote on last edited by
                          #12

                          @Christian-Ehrlicher what if both succeed?

                          1 Reply Last reply
                          0
                          • Christian EhrlicherC Christian Ehrlicher

                            Start both directly instead waiting for the first one.

                            U Offline
                            U Offline
                            user4592357
                            wrote on last edited by
                            #13

                            @Christian-Ehrlicher what about https://doc.qt.io/qt-5/qprocess.html#execute?

                            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