can't receive data via TcpSocket but can send!



  • Hi All
    I have a little problem with QTcpSocket.
    I made a socket and I connect to a server which has made by-- tcp server --pc software .
    I send data to server but I cant receive data !



  • @ali1377
    connect(tcpSocket, SIGNAL(connected()),this, SLOT(tcpConnected()));
    connect(tcpSocket, SIGNAL(disconnected()),this, SLOT(tcpDisconnected()));
    connect(tcpSocket, SIGNAL(bytesWritten(qint64)),this, SLOT(tcpBytesWritten(qint64)));
    connect(this, SIGNAL(newTCPData(QStringList)), this, SLOT(Draw_new_set_of_Data(QByteArray*)));
    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readTCPData()));
    connect(tcpSocket,QOverloadQAbstractSocket::SocketError::of(&QAbstractSocket::error),
    this, &My_drawing_object::displaySocketTCPConnectionError);

    ///Try Connection To Device
    tcpSocket->abort();
    tcpSocket->connectToHost("192.168.1.110",48569);
    

    I defined readTcpData but when I click on send button in Tcp Client Server(pc software for making tcp server) I cant receive data.but my sended data received at software correctly.but why?


  • Qt Champions 2018

    Hi @ali1377

    is readTCPData() a slot?

    Also, you don't check the return value of connect. Better convert the connects to the new style functor connects, so the compiler checks for you.

    Regards



  • @aha_1980
    hi @aha_1980
    yes That is a slot.
    I changed connect style to
    QObject::connect(
    tcpSocket, &QTcpSocket::readyRead,
    = { qDebug() << "GOT DATA " << tcpSocket->readAll(); }
    );
    but it didn't help me. Still I can't receive data.
    you can see I could send my data to server but can't send to my app from server.(when I click on send button in software has shown below)
    ![0_1568274672557_Capture.PNG] (https://ddgobkiprc33d.cloudfront.net/1e705b96-0c05-4b6e-87a1-50f708dd24ae.PNG)


  • Qt Champions 2018

    @ali1377

    • But is the receive slot called at all?
    • Where is tcpSocket defined?
    • Can you show where you create tcpSocket?
    • Why do you call tcpSocket->abort();?


  • @aha_1980
    actually this code is not mine.
    but I can show you any part of this code .I can't uplode whole code because its it's more than 9000 line.
    I have defined in constructor .

    My_drawing_object::My_drawing_object(QObject *parent) : QObject(parent), tcpSocket(new QTcpSocket(this)){

    tcpSocket = new QTcpSocket(this);

    //connect(timer, SIGNAL(timeout()), this, SLOT(:Draw_new_set_of_Data(QByteArray* dataRecieved)),static_cast<Qt::ConnectionType>(Qt::UniqueConnection));
    
    
    requestNewTCPConnection();
    //connect(tcpSocket, &QIODevice::readyRead, this, &My_drawing_object::readDataOverTCPConnection);
    
    QNetworkConfigurationManager manager;
     if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
         // Get saved network configuration
         QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
         settings.beginGroup(QLatin1String("QtNetwork"));
         const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
         settings.endGroup();
    
         // If the saved network configuration is not currently discovered use the system default
         QNetworkConfiguration config = manager.configurationFromIdentifier(id);
         if ((config.state() & QNetworkConfiguration::Discovered) !=
             QNetworkConfiguration::Discovered) {
             config = manager.defaultConfiguration();
         }
    
         networkSession = new QNetworkSession(config, this);
         connect(networkSession, &QNetworkSession::opened, this, &My_drawing_object::TCPConnectionSessionOpened);
    
         //getFortuneButton->setEnabled(false);
         //statusLabel->setText(tr("Opening network session."));
         networkSession->open();
     }
    

    }

    I don't know usage of abort .
    I think when I send a packet to app it must show qdebug Line has defined in connect part.


  • Qt Champions 2018

    @ali1377 said in can't receive data via TcpSocket but can send!:

    My_drawing_object::My_drawing_object(QObject *parent) : QObject(parent), tcpSocket(new QTcpSocket(this)){
    tcpSocket = new QTcpSocket(this);

    Do you see this? You already create the QTcpSocket twice, discarding the first object (memory leak!).

    You should make sure that tcpSocket is not destroyed somewhere in your program (e.g. because My_drawing_object is destroyed).

    Also two suggestions:

    • Name member variables with leading m_ (e.g. m_tcpSocket), that makes it easier for reviewers
    • For the class name I'd suggest MyDrawingObject


  • @aha_1980
    thanks for suggestions .I corrected second define of tcpSocket.
    During the run I can send data to server and it shows the socket exists and didn't destroy.
    so ..... .



  • @ali1377
    I tried with PacketSender too. but it showed "couldn't connect".
    where is problem.?
    I sent to IP ,PORT which app connected with them to server made by TCP client Server software
    0_1568284772974_Capture.PNG



  • @ali1377

    I tried with PacketSender too. but it showed "couldn't connect".
    I sent to IP ,PORT which app connected with them to server made by TCP client Server software

    • You show a screenshot with "Could not connect" on port 41025.

    • In the same shot a different piece of software is looking at port 48569, and that is what your code uses.

    ?

    What does your code slot do for disconnected and/or error? Make sure you are sure they are not being hit?

    qDebug() << "GOT DATA " << tcpSocket->readAll();

    The incoming data is not text characters. Do you mean you never see the GOT DATA or you just do not see anything to the right of it?



  • @jonb
    thanks for your reply.The problem was changing the port.I sent my packets to server which has made by TcpClientServer .The software shows ip and port .but I must notice that this port is not bound to socket and it can change .so now I bound a port like 4000 to socket and now I can receive packets.



  • @ali1377
    In my idea maybe with connecting to a server made by my code with nextPendingConnection function we don't loose port but with this software it change. That is my idea and maybe it's no true.


 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.