PDA

View Full Version : waitForBytesWritten



smalls
13th January 2006, 14:34
hi all,

i am writing a client server application and my server uses a thread per client,
writing to the socket as follows



socket.write(block) // block is of type QByteArray
socket.waitForBytesWritten(-1)


does the command in the second line enforce all data t be actually written to the socket, which was in the block variable?
the docu says just "it waits until a payload of data has been written".

greetings

smalls

high_flyer
13th January 2006, 14:43
I don't thik it can 'enforce' anything, it only returns once data has been written, and will not reutrn as long as its not so.

smalls
13th January 2006, 19:42
so, is there a way to tell whether it's all send. or maybe some function returning the amount of data that is still in the socket's buffer?

high_flyer
13th January 2006, 20:11
so, is there a way to tell whether it's all send.
Yes, as the docs say:

...Returns true if a payload of data was written to the device;
So, if it returns false, not all data was written.
You can use a timeout and ask bytesToWrite () to see howmany bytes are still wating to be written.

klgron
1st March 2006, 12:12
The data is written to the socket.

My question is, is also sent to the receiver?

Can the Nagel algorithm have something to do with this?
How to turn that of on a Windows XP system?

Regards

zlatko
1st March 2006, 12:28
What exactly you want to do?
And what is the Nagel algoritm ? :confused:

high_flyer
1st March 2006, 12:30
In order to know if this has to do with the Nagel algorithm you could just flush and see if it gets to the receiver.

klgron
1st March 2006, 13:40
I want to send a large file and when the total file is sent I just want to send an ack.

I Want the ack in a new TCP message. I have hard to find the ack when sniffing the network, once i found it last in the last "file" TCP message. Now, when introduced the flush, I cant find the ack anywhere.



#define TCP_MAXBUF 0x08000 // max TCP send and receive size buffer 32768 (MAX right now)

// Send File
do
{
// read file pattly
qint64 bytesRead = vrFile->read((char *)buffer,TCP_MAXBUF);

int sentSize = pFPSocket->write(fileBuffer.data(), bytesRead);
pFPSocket->flush();
if(!pFPSocket->waitForBytesWritten(Timeout))
{
theLog->WriteLog(QString("Wait for bytes written timed out"),LOG_LEVEL_ERR);
}
}while(!vrFile->atEnd()) ;

// Send Ack
QByteArray ackBuffer(7, 0);
QDataStream outAck(&ackBuffer, QIODevice::WriteOnly);

outAck << (unsigned char)TCP_DISCONNECT;
outAck << (quint32)1;
outAck << (unsigned char)"¤";

int i = sizeof(TCPPacket);
if(pFPSocket->write(ackBuffer.data(),6) < 6 )
{
// ERROR not total ack sent
theLog->WriteLog(QString("Could not store %1, complete ack not sent.").arg(Filename),LOG_LEVEL_ERR);
statusOK = false;
}

pFPSocket->flush();
if(!pFPSocket->waitForBytesWritten(Timeout))
{
// ERROR not total ack sent
theLog->WriteLog(QString("Could not store %1, ack bytes not written.").arg(Filename),LOG_LEVEL_ERR);
statusOK = false;
}


Thanks

high_flyer
1st March 2006, 17:42
Try this (I added another flush() between the "file" and "ack" packets):.



#define TCP_MAXBUF 0x08000 // max TCP send and receive size buffer 32768 (MAX right now)

// Send File
do
{
// read file pattly
qint64 bytesRead = vrFile->read((char *)buffer,TCP_MAXBUF);

int sentSize = pFPSocket->write(fileBuffer.data(), bytesRead);
pFPSocket->flush();
if(!pFPSocket->waitForBytesWritten(Timeout))
{
theLog->WriteLog(QString("Wait for bytes written timed out"),LOG_LEVEL_ERR);
}
}while(!vrFile->atEnd()) ;
//flush the file packet
pFPSocket->flush();
// Send Ack
QByteArray ackBuffer(7, 0);
QDataStream outAck(&ackBuffer, QIODevice::WriteOnly);

outAck << (unsigned char)TCP_DISCONNECT;
outAck << (quint32)1;
outAck << (unsigned char)"¤";

int i = sizeof(TCPPacket);
if(pFPSocket->write(ackBuffer.data(),6) < 6 )
{
// ERROR not total ack sent
theLog->WriteLog(QString("Could not store %1, complete ack not sent.").arg(Filename),LOG_LEVEL_ERR);
statusOK = false;
}

pFPSocket->flush();
if(!pFPSocket->waitForBytesWritten(Timeout))
{
// ERROR not total ack sent
theLog->WriteLog(QString("Could not store %1, ack bytes not written.").arg(Filename),LOG_LEVEL_ERR);
statusOK = false;
}