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();
client_socket->write(packet,packet.size());
client_socket->waitForBytesWritten();
To copy to clipboard, switch view to plain text mode
above code is not work correctly,and then i use this code for sending:
if (packet.size()>25344)
{
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();
}
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();
}
To copy to clipboard, switch view to plain text mode
and in another hand i using this code for reading data :
void server::readyRead()
{
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 //...
}
}
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
//...
}
}
To copy to clipboard, switch view to plain text mode
second way is better,but in both of tow methods ,QTcpSocket terminated after sending some bytes!
what's the problem!?
Bookmarks