PDA

View Full Version : The QTcpSocket has been terminated when writing large data



ms2222
5th December 2014, 21:01
When i writing large data,for example 18MB data over QTcpSocket,the QTcpSocket terminated in Writing process...
first i used this code for writing:


client_socket->write(packet,packet.size());
client_socket->waitForBytesWritten();
above code is not work correctly,and then i use this code for sending:

if (packet.size()>25344)
{
QDataStream data_stream(packet);
data_stream.setVersion(QDataStream::Qt_5_3);
while (!data_stream.atEnd())
{
int len=1024;
char buffer[len];
int size_read=data_stream.readRawData(buffer,len);
if (size_read==-1)
cout<<"ERROR!";
client_socket->write(buffer,size_read);
client_socket->waitForBytesWritten();
}
}
else
{
client_socket->write(packet,packet.size());
client_socket->waitForBytesWritten();
}

and in another hand i using this code for reading data :

void server::readyRead()
{

QTcpSocket *i_client=(QTcpSocket*) sender();
quint16 packet_size=i_client->bytesAvailable();//Read Count Bytes waiting on line
qDebug()<<"Packet Recived,Packet len: "<<QString::number(packet_size)<<"\n";

if (packet_size>0)
{

QByteArray recive_bytes=i_client->read(packet_size);//Read Packet
//...
}
}

second way is better,but in both of tow methods ,QTcpSocket terminated after sending some bytes!
what's the problem!?

ChrisW67
5th December 2014, 23:02
What is terminated? The sender or receiver? Terminated how? Crash or simply stop? Have you single-stepped through the code in your debugger or inspected the stack backtrace?

In your second sender what occurs after the byte array is exhausted and your read fails?

What happens at the server when there are more than 65536 bytes available (line 5)? What is the return type of QTcpSocket::bytesAvailable()?


Do you really want to block your entire program while 18MB is sent?

ms2222
6th December 2014, 09:56
oh,thank's,it's my fault,QTcpSocket::bytesAvailable() return qint64 but i used quint16 !

Do you really want to block your entire program while 18MB is sent?
yes,in this project is not care program is block...