Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Multiple file sending through QTcpSocket and QTcServer



  • Hi,
    I am trying to send multiple test files through Qtcpsocket from the client side (using QDatastream and QBytearray)and receiving the same from the server side by implementing incomingconnection(). From the client side I am sending the file name and the total block size of the file data being sent to the server. As of now I am sending text files from the client. I tested the logs from the client side. It is sending the files as expected.
    But on the server side it is not reading according to the block size being given to the socket read. Because of that files are getting mixed. I mean the text of one file being appended to another text file.
    In the server once I receive the number of data which is equal to the block size ,I am appending to one file with the same name as received.On the server side ,I have implemented the multi threading functionality to receive all the data chunks.
    can anybody suggest some workaround for the issue.


  • Lifetime Qt Champion

    @sunilpkd Why do you use block size instead of file size in bytes?



  • @jsulm I create only one socket from client side.Is itok? or should I create the socketdescriptor in client side? Here Server and client will be running in different machine.I am sending the file content using the same socket..I guess I made a mistake in identifying the sockets at the client side and server side..Can u give some idea..


  • Lifetime Qt Champion

    @sunilpkd said in Multiple file sending through QTcpSocket and QTcServer:

    I create only one socket from client side.Is itok?

    Yes, if you send one file after another (not in parallel).
    "I guess I made a mistake in identifying the sockets at the client side and server side" - there is nothing to identify. You get a socket for each connection on the server side, and the client sends the file name, so I don't see a problem here.



  • ok..thank you..I am doing in the same manner. This is my application..I am selecting 5 text filesthrough client application to be sent to server.Those files are sent from client using QDatastream.
    But on the server when I call tcpsocket->bytesavailabe(),it is giving the content from more than one file at a time.After the bytesavailable function call, I am reading the content using QDatastream.

    So How can I expect whenenever I call tcpsocket->byteavailable() from server side, it will give the content only wrt oe file at a time rather than appending the next file information


  • Lifetime Qt Champion

    @sunilpkd Sounds like you are doing something wrong on server side, but since you did not post your code I have no idea what exactly.

    "But on the server when I call tcpsocket->bytesavailabe(),it is giving the content from more than one file" - how is that possible? bytesAvailable() returns number of bytes available, not the actual bytes. So, what do you mean? What are you doing on the server side?

    "So How can I expect whenenever I call tcpsocket->byteavailable() from server side" - there is nothing to expect. Simply connect to readyRead() signal and read the data there...



  • This post is deleted!


  • @jsulm
    Hi it is observed first time when the run the application, it is sending the files to the server correctly. but on second time when I launch the same application, it sends fewer number of files.
    In the server part, I am creating the QThread in the following way
    void ComServer::incomingConnection(qintptr handle)
    {

    qDebug() << Q_FUNC_INFO << " new connection";
     thread = new ComThread(handle,this);
    
      connect(thread, SIGNAL(newFileAdded()), this, SLOT(newFileReceiving()));
      connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
         connect(thread, SIGNAL(stopthread()), this, SLOT(stopoperation()));
    
    
      thread->start();
    

    }

    From the QThread class following are the connect parameters

    void ComThread::run()
    {
    // qDebug()<<"FortuneThread::run()"<<endl;
    tcpSocket=new QTcpSocket;
    if (!tcpSocket->setSocketDescriptor(socketDescriptor)) {
    emit error(tcpSocket->error());
    return;
    }

    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onReadyRead()), Qt::DirectConnection);
    connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(onDisconnected()), Qt::DirectConnection);
    

    qDebug()<<"SocketDescriptor is ::"<<socketDescriptor<<endl;

    exec();

    }

    Once I get the disconnected() signal when the socket is not available, what is the order of freeing the resources?
    Currently I am doing the following way

    1.Once I get the disconnected() signal from the ComThread class , I am emitting a signal to ComServer class. It will catch up the signal and then will sop the thread and then close the QTcpServer .

    Is it ok to free the resources in the above way? Thank you..


  • Lifetime Qt Champion

    @sunilpkd Why do you have a thread? Is there really a need for it? Even if there is need for a thread you should first implement without second thread, make sure it works and then add threading.



  • @jsulm
    So you mean to say that for the time being since I run only one client(so only one socket from the client side) from another machine to send the text files, I don't need to create different thread to handle the same. So If I connect from multiple sockets , then only I need to implement the QThread, am I right?


  • Lifetime Qt Champion

    @sunilpkd You do not need threads even for more than one connection! So, no you do not need threads if you accept more than one connection, I don't know why you think you need threads. Qt is an asynchronous framework and you can handle several connection in one threads without issues. You only need threads if you have too many connections to handle them in one thread.
    Did you check https://doc.qt.io/qt-5/qtnetwork-fortuneserver-example.html ?



  • @jsulm

    I got the output. I implemented the server part without using thread since it was only expecting from one client. From the client side, I was streaming the text files continuously without any delay. So while receiving at the server side, the content was getting mixed up.. Then I tried to put some delay over the client after sending each file. Following that each file was received at the server part without any corruption...Thank you for your guidance.. May I know without adding the delay on the client side, is there any otherway to achieve the same..


Log in to reply