QTcpSocket not sending data
-
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?
-
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.
-
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?
-
the "write":http://qt-project.org/doc/qt-5.1/qtcore/qiodevice.html#write returns number of bytes written