You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Friday, September 28th 2012, 9:10pm

problem while sending file using QTcpSocket

Hello everybody,
I’m looking for an example about sending a file from one pc to an other with QTcpSocket. I tried to create my own code. I have an application, in which, the user will choose a file from his DD ( all types) and send it to the TcpServer, this server will then send this file to the other clients.But, I have a problem, when i choose the file and i send it, in the client’s side, i have this message: file is sending , but in the server’s side, it shows me that the file isn’t recieved with it’s totaly bytes.
Any suggestion please. This is the function for sending the file in the client’s side:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void FenClient::on_boutonEnvoyer_2_clicked()
{
    QString nomFichier = lineEdit->text();
        QFile file(lineEdit->text());
        if(!file.open(QIODevice::ReadOnly))
        {
            qDebug() << "Error, file can't be opened successfully !";
            return;
 
        }
 
        QByteArray bytes = file.readAll();
 
        QByteArray block;
        QDataStream out(&block, QIODevice::WriteOnly);
 
        out << quint32(0);
        out << nomFichier;
        out << bytes;
        out.device()->seek(0);
        out << quint32((block.size() - sizeof(quint32)));
 
        qDebug() << "Etat : envoi en cours...";
         listeMessages->append("status : sending the file...");
 
        socket->write(block);
}

and the server side:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "FenServeur.h"
 
void FenServeur::datarecieved()
{
 
    QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
 
        if(socket == 0)
        {
            qDebug() << "no Socket!";
            return;
        }
 
        forever
        {
            QDataStream in(socket);
            if(blockSize == 0)
            {
                if(socket->bytesAvailable()  )
                {
                    qDebug() << "Error < sizeof(quint32))";
                    return;
                }
 
                in >> blockSize;
            }
 
            if(socket->bytesAvailable() < blockSize)
             {
                qDebug() << "data not recieved with its total bytes";
 
                return;
             }
 
            qDebug() << "!!!!!!";
            QByteArray dataOut;
            QString nameFile;
            in >> nameFile >> dataOut;
            QFile fileOut(nameFile);
            fileOut.open(QIODevice::WriteOnly);
            fileOut.write(dataOut);
            fileOut.close();
 
            blockSize = 0;
         }
}
 
void FenServeur::sendToAll(const QString &message)
{
 
    QByteArray paquet;
    QDataStream out(&paquet, QIODevice::WriteOnly);
 
    out << (quint32) 0;
    out << message;
    out.device()->seek(0);
    out << (quint32) (paquet.size() - sizeof(quint32));
    for (int i = 0; i < clients.size(); i++)
    {
        clients[i]->write(paquet);
    }
 
}

Thanks in advance ;(