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. QTcpSocket not sending data
Forum Updated to NodeBB v4.3 + New Features

QTcpSocket not sending data

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 3.3k 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.
  • K Offline
    K Offline
    kloveridge
    wrote on last edited by
    #1

    I am new to socket programming. I am writing a Maya 2014 (for OSX) plugin that demonstrates communicating to a QT application outside of maya.

    My outside qt application acknowledges that the socket is connected to Maya. But I don't ever get any data through! I've been at this for hours. Any help would be appreciated. Here's the code on the Maya plugin side:

    @Client::Client(QObject *parent) : QObject(parent)
    {
    m_socket = new QTcpSocket(this);

    connect(m_socket, SIGNAL(connected()),this, SLOT(connected()));
    connect(m_socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
    connect(m_socket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64)));
    connect(m_socket, SIGNAL(readyRead()),this, SLOT(readyRead()));
    
    MGlobal::displayInfo("Connecting to external program...\n");
    
    m_socket->connectToHost(QHostAddress::LocalHost, 1236);
    if(!m_socket->waitForConnected(5000))
    {
        QString error = m_socket->errorString();
        MGlobal::displayError("Connection error:");
    } else
        MGlobal::displayInfo("Connected to 1236\n");
    

    }

    void Client::connected()
    {
    MGlobal::displayInfo("connected...\n");
    }

    void Client::disconnected()
    {
    MGlobal::displayInfo("disconnected...\n");
    }

    void Client::bytesWritten(qint64 bytes)
    {
    MGlobal::displayInfo(MString("bytes written...") + bytes);
    }

    void Client::readyRead()
    {
    MGlobal::displayInfo("Reading...\n");
    QByteArray buffer;
    buffer = m_socket->readAll();
    QString theLog( buffer );
    MGlobal::displayInfo("Got something...\n");
    }

    void Client::sendMessage( )
    {
    MGlobal::displayInfo("About to send message..");
    // return;

    QString cmd("Hello from Maya!");
    if( m_socket && m_socket->state() == QAbstractSocket::ConnectedState  ) {
        MGlobal::displayInfo("Looks like its going to work...");
       // m_socket->write(cmd.toUtf8());
       m_socket->write("hello");
       m_socket->write("\n");
    } else
        MGlobal::displayWarning("socket isn't hooked up!");
    

    }
    @

    the "m_socket->write("hello");" line 54 is where I don't see "hello" in my external application. The odd thing is, when Client() is instantiated, my application does accept the Maya connection. It appears that we are connected.

    Any obvious issues with my code?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Might be a silly question but: when is sendMessage called ?

      You should also check that the write function is currently writing the right amount of data.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kloveridge
        wrote on last edited by
        #3

        This is a Maya plugin. Client() is a instantiated objected that is allocated when the plugin is loaded. You are not seeing the part which invokes the call. But I am certain that sendMessage() is called.

        bq. You should also check that the write function is currently writing the right amount of data.

        How do I check this?

        1 Reply Last reply
        0
        • IamSumitI Offline
          IamSumitI Offline
          IamSumit
          wrote on last edited by
          #4

          [quote author="kloveridge" date="1396217810"]
          How do I check this?[/quote]

          Use qDebug() m_socket->write("hello"); will return the amount of written data in bytes.

          Be Cute

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            the "write":http://qt-project.org/doc/qt-5.1/qtcore/qiodevice.html#write returns number of bytes written

            Vote the answer(s) that helped you to solve your issue(s)

            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