Reputation: 14738
I try to communicate via TCP Socket between a QT4-Application (MyApp) and Cayuga (written in C++).
The connection part works fine, i.e. Cayuga connects to MyApp.
Now, MyApp is sending some data to Cayuga, but nothing is received.
void MyApp::init()
QTcpServer *m_server;
QTcpSocket *clientConnection;
//Open socket for transmission
m_server = new QTcpServer(this);
if (!m_server->listen(QHostAddress::Any, m_port)) {
//Error handling
return;
}
connect(m_server, SIGNAL(newConnection()), this, SLOT(startSend()));
void MyApp::startSend()
{
clientConnection = m_server->nextPendingConnection();
}
The writting is done here:
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << s;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
clientConnection->write(block);
clientConnection->flush();
My tutor suggested to use an external library (cudb) if I cannot get it to work with QTcpSockets. That does not feel right and that's why I hope you have a better answer to my problem.
Upvotes: 1
Views: 629
Reputation:
This is my guess of what's happening:
QDataStream implements a serializing protocol (Hence having to specify a version (Qt_4_0) for it). You need something on the other end that understands that protocol (to wit, another Qt_4_0 DataStream). Particularly, QDataStream makes sure you get the right data regardless of the endianness of the sending and receiving ends.
Instead of serializing to a block and then writing the block, you can try something like:
QDataStream out(clientConnection, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out.writeRawData(data, length);
clienConnection->flush();
writeRawData() does not marshall your data...
Upvotes: 1