
Originally Posted by
wirasto
Not work. But thank's
if( !socket.setSocketDescriptor( m_descriptor ) )
{
qDebug( "Socket error!" );
return;
}
QFile file("images/linux.png");
{
qDebug("Cannot open file!");
return;
}
int size=data.size();
for(int i=0; i< 4; ++i)
{
quint8 b=(size >> (i*8)) & 0x000000ff;
data.append(b);
}
socket.write(data);
socket.disconnectFromHost();
QTcpSocket socket;
if( !socket.setSocketDescriptor( m_descriptor ) )
{
qDebug( "Socket error!" );
return;
}
QFile file("images/linux.png");
if (!file.open(QIODevice::ReadOnly))
{
qDebug("Cannot open file!");
return;
}
QByteArray data=file.readAll();
int size=data.size();
for(int i=0; i< 4; ++i)
{
quint8 b=(size >> (i*8)) & 0x000000ff;
data.append(b);
}
socket.write(data);
socket.disconnectFromHost();
To copy to clipboard, switch view to plain text mode
Men you have appended your size to the end of the data! What is the sense of doing that if you have to know the size at the very beginning of receiving data??? You have to send the size, and then the data:
if( !socket.setSocketDescriptor( m_descriptor ) )
{
qDebug( "Socket error!" );
return;
}
QFile file("images/linux.png");
{
qDebug("Cannot open file!");
return;
}
int size=data.size();
for(int i=0; i< 4; ++i)
{
quint8 b=(size >> (i*8)) & 0x000000ff;
sizeBytes.append(b);
}
sizeBytes.append(data);
socket.write(sizeBytes);
socket.disconnectFromHost();
QTcpSocket socket;
if( !socket.setSocketDescriptor( m_descriptor ) )
{
qDebug( "Socket error!" );
return;
}
QFile file("images/linux.png");
if (!file.open(QIODevice::ReadOnly))
{
qDebug("Cannot open file!");
return;
}
QByteArray data=file.readAll();
int size=data.size();
QByteArray sizeBytes;
for(int i=0; i< 4; ++i)
{
quint8 b=(size >> (i*8)) & 0x000000ff;
sizeBytes.append(b);
}
sizeBytes.append(data);
socket.write(sizeBytes);
socket.disconnectFromHost();
To copy to clipboard, switch view to plain text mode
it is not that hard to invent that idea.
And you dont even use that information about size on the client side.
Tell me what happend if you do it like this:
void Dialog::tcpReady()
{
int bytesAvail = socket.bytesAvailable() - 4;
quint32 size=0;
for(int i=0; i < 4; ++i)
{
quint8 b=ar.at(i);
size |= (((quint32)b) & 0x000000ff) << i*8;
}
if (size > bytesAvail)
qDebug("Different sizes!!!");
else
qDebug("All data have been delivered!");
ar = socket.readAll();
QFile file("linuxmu.png");
file.write(ar);
file.close();
}
void Dialog::tcpReady()
{
int bytesAvail = socket.bytesAvailable() - 4;
QByteArray ar=socket.read(4);
quint32 size=0;
for(int i=0; i < 4; ++i)
{
quint8 b=ar.at(i);
size |= (((quint32)b) & 0x000000ff) << i*8;
}
if (size > bytesAvail)
qDebug("Different sizes!!!");
else
qDebug("All data have been delivered!");
ar = socket.readAll();
QFile file("linuxmu.png");
file.open(QIODevice::WriteOnly);
file.write(ar);
file.close();
}
To copy to clipboard, switch view to plain text mode
I think not right to me for learn about QtNetwork now.
As I see you have to read about network at all...
Bookmarks