Unsolved cannot write through opened socket
-
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, theisConnectionAvailable()
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.
-
Please take a look at the QTcpServer example. Your code does ... nothing really useful.
-
@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; }
-
This code can not work. Follow the QTcpServer examples.
-
@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? -
@Christian-Ehrlicher my bad
the other app expects file descriptors, and i'm providing socket port
so, how should i work with that app? -
@user4592357 said in cannot write through opened socket:
so, how should i work with that app?
It's your app - how should we know?
-
@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? -
@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
-
@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? }); }
-
Start both directly instead waiting for the first one.
-
@Christian-Ehrlicher what if both succeed?
-