Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Multiple file sending through QTcpSocket and QTcServer
Forum Updated to NodeBB v4.3 + New Features

Multiple file sending through QTcpSocket and QTcServer

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 2 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sunilpkd
    wrote on last edited by sunilpkd
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • S sunilpkd

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sunilpkd
        wrote on last edited by
        #3

        @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..

        jsulmJ 1 Reply Last reply
        0
        • S sunilpkd

          @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..

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sunilpkd
            wrote on last edited by
            #5

            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

            jsulmJ 1 Reply Last reply
            0
            • S sunilpkd

              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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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...

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply
              1
              • S Offline
                S Offline
                sunilpkd
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @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...

                  S Offline
                  S Offline
                  sunilpkd
                  wrote on last edited by
                  #8

                  @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..

                  jsulmJ 1 Reply Last reply
                  0
                  • S sunilpkd

                    @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..

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @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.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    S 1 Reply Last reply
                    1
                    • jsulmJ jsulm

                      @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.

                      S Offline
                      S Offline
                      sunilpkd
                      wrote on last edited by
                      #10

                      @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?

                      jsulmJ 1 Reply Last reply
                      0
                      • S sunilpkd

                        @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?

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @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 ?

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        S 1 Reply Last reply
                        3
                        • jsulmJ jsulm

                          @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 ?

                          S Offline
                          S Offline
                          sunilpkd
                          wrote on last edited by
                          #12

                          @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..

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved