×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    6

    Red face help sending file from PC to mobile

    im about sending an XML file into mobile. so file is transfered to mobile using wireless access point. im using fortune server and client(default sample from qt) to try it.

    i got this code on fortune server to change
    Code:
     void Server::sendFortune()
     {
         QByteArray block;
         QDataStream out(&block, QIODevice::WriteOnly);
         out.setVersion(QDataStream::Qt_4_0); out << (quint16)0;
         out << fortunes.at(qrand() % fortunes.size());
         out.device()->seek(0);
         out << (quint16)(block.size() - sizeof(quint16));
    QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
         connect(clientConnection, SIGNAL(disconnected()),
                 clientConnection, SLOT(deleteLater()));clientConnection->write(block);
         clientConnection->disconnectFromHost();
     }
    this function change data into Byte. if i want to send file, i must change file into byte. Anyone can help me how to change file into byte? ive done this but i dont know how to test.
    Code:
    void Server::sendFortune()
    {
        //QByteArray block;
        //QByteArray block("chibineji.jpg");
        QFile file("test.dat");
        file.open(QIODevice::WriteOnly);
        QDataStream filein(&file);
        QByteArray block;
        QDataStream out(&block, QIODevice::WriteOnly);
        out.setVersion(QDataStream::Qt_4_0);
    
        out << (quint16)0;
        //out << fortunes.at(qrand() % fortunes.size());
        //out << QString("what is it?");
        //out << (qint32)42;
        //out.~QDataStream(filein);
        out.device()->seek(0);
        out << (quint16)(block.size() - sizeof(quint16));
    
        QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
        connect(clientConnection, SIGNAL(disconnected()),
                clientConnection, SLOT(deleteLater()));
        clientConnection->write(block);
        clientConnection->disconnectFromHost();
    }
    my code is about adding variable filein(QDataStream) into out(QDataStream). but i havent find how to do it.
    Last edited by forent; 2010-03-05 at 01:29. Reason: adding words

  2. #2
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: help sending file from PC to mobile

    There is no such thing as Byte. You can do something like this:

    Code:
    #define BUFFER_SIZE 256
    char buffer[BUFFER_SIZE];
    QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    
    QFile file("file.dat");
    file.open(QIODevice::ReadOnly);
    while(0 < file.read(buffer, BUFFER_SIZE)) {
       clientConnection->write(buffer, BUFFER_SIZE);
    }
    file.close();

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    6

    Smile Re: help sending file from PC to mobile

    thank you very much.. my prog is now work..thanks to you..with a little modification
    Code:
    void Server::sendFortune()
    {
    //! [4.5]
        QString namafile = "sample.txt";
        QString namafilecopy = "sample_copy.txt";
        QFile file(namafile);
        QByteArray block;
    
        if(file.exists(namafile)){
            if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                  return;
            file.seek(0);
            fortunes.clear();
            qDebug() << fortunes;
            while (!file.atEnd()) {
                  block = file.readLine();
                  fortunes << tr(block);
                  qDebug() << "isi fortunes" << fortunes;
              }
            if(QFile::copy(namafile,namafilecopy)){
                qDebug() << "file copy sukses";
            }else{
                qDebug() << "file copy gagal";
            }
            //file.copy(namafile, "/backup/test.txt");
            qDebug() << QFile::CopyError ;
            file.close();
            //file.remove();
        }else{
            qDebug() << namafile << " tidak ada.";
            return;
        }
    
    //! [5]
    
        QDataStream out(&block, QIODevice::WriteOnly);
        out.setVersion(QDataStream::Qt_4_0);
    //! [4] //! [6]
        out << (quint16)0;
        //out << fortunes.at(qrand() % fortunes.size());
        out << fortunes.at(0) ;
        out.device()->seek(0);
        out << (quint16)(block.size() - sizeof(quint16));
    //! [6] //! [7]
    
        QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
        connect(clientConnection, SIGNAL(disconnected()),
                clientConnection, SLOT(deleteLater()));
    //! [7] //! [8]
    
        clientConnection->write(block);
        clientConnection->disconnectFromHost();
    
    //! [5]
    }

Similar Threads

  1. Stream Video File from Mobile to PC
    By mobileteam in forum Mobile Java Media (Graphics & Sounds)
    Replies: 0
    Last Post: 2009-12-24, 05:58
  2. receive file from bluetooth (Mobile -> PC)
    By caucuk33 in forum Bluetooth Technology
    Replies: 4
    Last Post: 2008-07-11, 13:20
  3. File transfer from mobile to PC
    By iworktech_kiran in forum Mobile Java General
    Replies: 1
    Last Post: 2007-09-11, 13:54
  4. Contacts fields order??
    By timatima in forum Symbian
    Replies: 1
    Last Post: 2007-06-08, 13:51
  5. Replies: 1
    Last Post: 2005-05-26, 14:22

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×