Hi, I am trying to create a basic videoconference program for an university assignment.
I am using Qt 4.5.2 on Linux.
When I send full images (something like 230k) over the socket it waits in the socket.disconnect() on the sender side and on the receiver side it only goes to 170k or so of bytesAvailable().
I am sending images through QTcpSocket like this:
Sender's side:
int NetworkSender::sendData (char *image, int dataSize, unsigned short port)
{
int return_value;
socket.connectToHost(remoteHost, port);
out << (quint32)0;
out.writeBytes((const char*)image, dataSize);
out.device()->seek(0);
out << (quint32)(block.size() - sizeof(quint32));
return_value = socket.write(block);
//socket.flush();
//socket.waitForBytesWritten(20);
socket.disconnectFromHost();
socket.waitForDisconnected();
if (return_value != -1) {
return 0;
}else {
return -1;
}
}
int NetworkSender::sendData (char *image, int dataSize, unsigned short port)
{
QTcpSocket socket;
int return_value;
socket.connectToHost(remoteHost, port);
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint32)0;
out.writeBytes((const char*)image, dataSize);
out.device()->seek(0);
out << (quint32)(block.size() - sizeof(quint32));
return_value = socket.write(block);
//socket.flush();
//socket.waitForBytesWritten(20);
socket.disconnectFromHost();
socket.waitForDisconnected();
if (return_value != -1) {
return 0;
}else {
return -1;
}
}
To copy to clipboard, switch view to plain text mode
On the receiver side I have a thread, like this:
void NetworkReceiverThread::run()
{
// socket.setReadBufferSize(500000);
int timeout = 2 * 1000;
if (!socket.setSocketDescriptor(socketDescriptor)) {
emit error(socket.error(), socket.errorString());
return;
}
while (socket.bytesAvailable() < (int)sizeof(quint32)) {
if (!socket.waitForReadyRead(timeout)) {
emit error(socket.error(), socket.errorString());
return;
}
}
quint32 blockSize;
in >> blockSize;
while (socket.bytesAvailable() < blockSize) {
if (!socket.waitForReadyRead(timeout)) {
qDebug("4");
emit error(socket.error(), socket.errorString());
return;
}
}
qDebug("5");
unsigned int data_len;
in.readBytes(data, data_len);
qDebug("received data");
qDebug
(QString::number(data_len
).
toStdString().
data());
emit(done());
}
void NetworkReceiverThread::run()
{
QTcpSocket socket;
// socket.setReadBufferSize(500000);
int timeout = 2 * 1000;
if (!socket.setSocketDescriptor(socketDescriptor)) {
emit error(socket.error(), socket.errorString());
return;
}
while (socket.bytesAvailable() < (int)sizeof(quint32)) {
if (!socket.waitForReadyRead(timeout)) {
emit error(socket.error(), socket.errorString());
return;
}
}
quint32 blockSize;
QDataStream in(&socket);
in.setVersion(QDataStream::Qt_4_0);
in >> blockSize;
while (socket.bytesAvailable() < blockSize) {
if (!socket.waitForReadyRead(timeout)) {
qDebug("4");
emit error(socket.error(), socket.errorString());
return;
}
}
qDebug("5");
unsigned int data_len;
in.readBytes(data, data_len);
qDebug("received data");
qDebug(QString::number(data_len).toStdString().data());
emit(done());
}
To copy to clipboard, switch view to plain text mode
As I said if I try to put a debug on the reader inside " while (socket.bytesAvailable() < blockSize) {" printing bytes available I got something like 80k , 120k 160k and then it stops.
I have tried to set manually the buffer of the receiver as can be seen as a commented line, same result.
I have tried also calling flush() on the sender just after the write(), I got this:
QNativeSocketEngine::write() was not called in QAbstractSocket::ConnectedState
QAbstractSocket::waitForDisconnected() is not allowed in UnconnectedState
I have tried waitForBytesWritten() but same result
I am testing on loopback (127.0.0.1)
Bookmarks