Subclass of QTcpSocket not working properly under Linux
-
I'm trying to run a code that can run correctly under Windows, but I find it can't run under Linux, the slot never triggered.
void MainWindow::slotNewConnection() {
while (tcpserver->hasPendingConnections()) {
QTcpSocket *tcp = tcpserver->nextPendingConnection();
TcpPlatformSocket *tcpsocket = new TcpPlatformSocket;
tcpsocket->setSocketDescriptor(tcp->socketDescriptor());
connect(tcpsocket,&TcpPlatformSocket::readyRead,this,&MainWindow::slotReadyRead);
TcpPlatformSocketList.append(tcpsocket);
mode = MODE_WAIT_TYPE;
}
}void MainWindow::slotReadyRead() {
QObject* obj = sender();
TcpPlatformSocket* ptcpSocket = dynamic_cast<TcpPlatformSocket*>(obj);QByteArray buffer = ptcpSocket->readAll(); for(int i=0; i<buffer.length(); i++){ readStateMachine(ptcpSocket, buffer.at(i)); }}
class TcpPlatformSocket : public QTcpSocket
{
Q_OBJECT
public:
explicit TcpPlatformSocket(QObject *parent = nullptr);
void setPlatformType(platform_t platformType);
platform_t getPlatformType();private:
platform_t platformType;};
-
I'm trying to run a code that can run correctly under Windows, but I find it can't run under Linux, the slot never triggered.
void MainWindow::slotNewConnection() {
while (tcpserver->hasPendingConnections()) {
QTcpSocket *tcp = tcpserver->nextPendingConnection();
TcpPlatformSocket *tcpsocket = new TcpPlatformSocket;
tcpsocket->setSocketDescriptor(tcp->socketDescriptor());
connect(tcpsocket,&TcpPlatformSocket::readyRead,this,&MainWindow::slotReadyRead);
TcpPlatformSocketList.append(tcpsocket);
mode = MODE_WAIT_TYPE;
}
}void MainWindow::slotReadyRead() {
QObject* obj = sender();
TcpPlatformSocket* ptcpSocket = dynamic_cast<TcpPlatformSocket*>(obj);QByteArray buffer = ptcpSocket->readAll(); for(int i=0; i<buffer.length(); i++){ readStateMachine(ptcpSocket, buffer.at(i)); }}
class TcpPlatformSocket : public QTcpSocket
{
Q_OBJECT
public:
explicit TcpPlatformSocket(QObject *parent = nullptr);
void setPlatformType(platform_t platformType);
platform_t getPlatformType();private:
platform_t platformType;};
You copy the socket descriptor from one socket to another for no reason. Use the returned QTcpSocket instead creating a new one with the same socket descriptor.
-
You copy the socket descriptor from one socket to another for no reason. Use the returned QTcpSocket instead creating a new one with the same socket descriptor.
@Christian-Ehrlicher I want to use my TcpPlatformSocket instead of QTcpsocket
-
@Christian-Ehrlicher I want to use my TcpPlatformSocket instead of QTcpsocket
@ZZZCY
Since QTcpSocket *QTcpServer::nextPendingConnection() returns a baseQTcpSocketcreated by Qt intrastructure I think you would have to override void QTcpServer::incomingConnection(qintptr socketDescriptor) and create your own to achieve that.In which case it may not be worth the effort subclassing to create your own? Maybe your
TcpPlatformSocketcould just encapsulate instead? -
@ZZZCY
Since QTcpSocket *QTcpServer::nextPendingConnection() returns a baseQTcpSocketcreated by Qt intrastructure I think you would have to override void QTcpServer::incomingConnection(qintptr socketDescriptor) and create your own to achieve that.In which case it may not be worth the effort subclassing to create your own? Maybe your
TcpPlatformSocketcould just encapsulate instead?