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. Closing one socket closes multiple connections
Forum Updated to NodeBB v4.3 + New Features

Closing one socket closes multiple connections

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 6.8k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    DenisKormalev
    wrote on last edited by
    #3

    Yep, some small, but problem-showing code example will be good. Also I see that you are using stack variables, maybe problem is in variable visibility scope?

    1 Reply Last reply
    0
    • W Offline
      W Offline
      webmaster.skelton
      wrote on last edited by
      #4

      Ok here is one call:

      @ while(!file.atEnd())
      {

          QByteArray line = file.readLine();
          QString string(line);
          out << string;
      
          Communicator::uploadMedicalIdAccess_Socket.waitForConnected(200);
          Communicator::uploadMedicalIdGateWay_Socket.write(datablock);
          Communicator::uploadMedicalIdGateWay_Socket.waitForBytesWritten(200);
          Communicator::uploadMedicalIdGateWay_Socket.flush();
      }
      
      Communicator::uploadMedicalIdGateWay_Socket.waitForBytesWritten(2000);
      file.close();
      Communicator::uploadMedicalIdGateWay_Socket.disconnectFromHost();
      

      @

      This occurs when the file im reading in is ready

      the second call is similiar(nearly identical)

      @
      while(!file.atEnd())
      {

        QByteArray line = file.readLine();
        QString string(line);
        out << string;
      
        Communicator::uploadMedicalIdAccess_Socket.waitForConnected(200);
        Communicator::uploadMedicalidGateWayDocs_Socket.write(datablock);
        Communicator::uploadMedicalidGateWayDocs_Socket.waitForBytesWritten(200);
        Communicator::uploadMedicalidGateWayDocs_Socket.flush();
      }
      
      Communicator::uploadMedicalidGateWayDocs_Socket.waitForBytesWritten(200);
      file.close();
      Communicator::uploadMedicalidGateWayDocs_Socket.disconnectFromHost();
      

      }@

      I have the sockets declared at the class level so i can verify they close upon application close, but i am wondering if this is what is causing the problem.

      [edit: extra spaces removed / Denis Kormalev]

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DenisKormalev
        wrote on last edited by
        #5

        Code looks strange for me. You have one socket waiting for connection and write to other ones. Maybe it is because of your network protocol. But maybe some server-side problems causing sockets disconnection, not QTcpSocket problem?

        1 Reply Last reply
        0
        • W Offline
          W Offline
          webmaster.skelton
          wrote on last edited by
          #6

          These two functions are called only when they are needed, i wait to make sure the socket is connected to the server( which as of now is php, and im just starting to learn it enough to tweak it) once it is connected it writes to the server, to make sure all of the data is written we wait for bytes to be written, then we flush the socket. the second wait is not really needed, i just put it there to make me feel better lol. Then here is the real issue. disconnectFromHost should only terminate connection if there is no pending data to go out. if there is not, it closes. But if i have both sockets open at the same time, it kills both sockets.
          (by the way, this is only part of two separate functions) the rest is where i connect to the host and read in the xml file)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DenisKormalev
            wrote on last edited by
            #7

            For me this part of code looks correct (except strange using of uploadMedicalIdAccess_Socket object). Problem is probably lying somewhere in your server code or in another part of your client code.

            1 Reply Last reply
            0
            • W Offline
              W Offline
              webmaster.skelton
              wrote on last edited by
              #8

              Thanks for the input, ill have to take another, closer look at the server code.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #9

                It's hard to give any statement without knowing something about your protocol and PHP architecture. I have a guess, but will wait for some clarification from your side.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  webmaster.skelton
                  wrote on last edited by
                  #10

                  It was on the server side it was basically killing the server when the socket disconnected( I did not write the server, its just a quick and dirty script that i use for testing). I actually remember now when i had multiple sessions on the server, if one ended it disconnected the second client and killed the server. So since that is figured out, i do have another quick question.

                  I am basically testing to make sure i can retrieve data from the server and then write that data to a file. So for a test I write ("PING") to the server and in turn it sends back "this test worked". Now, to get that to the file I am storing i am setting the socket read to a QByteArray. I then try to write the QByteArray to a file( at this point i dont care what is written in there so long as something is) but the QByteArray remains empty. Here is the code

                  @ Communicator::retrieveMedicalIdAccess_Socket.connectToHost("192.168.1.15",9003,QAbstractSocket::ReadWrite);

                  Communicator::retrieveMedicalIdAccess_Socket.waitForConnected(200);
                  
                  
                  QDir dir = QDir::rootPath();
                  
                  QString path =dir.absoluteFilePath("");
                  
                  QFile file&#40;path&#41;;
                  
                  
                  QFile newfile(""&#41;;
                  
                  if(newfile.open((QIODevice::WriteOnly | QIODevice::Text&#41;))
                  {
                  
                  
                         file.remove();
                  
                         newfile.flush();
                         newfile.close();
                  
                         newfile.rename("","");
                  
                         file.flush();
                         file.close();
                  
                  
                  }
                  
                  Communicator::retrieveMedicalIdAccess_Socket.write("PING");
                  
                  Communicator::retrieveMedicalIdAccess_Socket.flush();
                  
                  Communicator::retrieveMedicalIdAccess_Socket.waitForReadyRead(2000);
                  

                  // Communicator::retrieveMedicalIdGateWay_Socket.read(500);

                  QByteArray data = Communicator::retrieveMedicalIdGateWay_Socket.read(200);
                  
                  
                  QString bin(data);
                  
                  newfile.open(QIODevice::ReadWrite);
                  if(newfile.isOpen())
                      QMessageBox::warning(NULL, "OPEN",bin );
                  else
                      QMessageBox::warning(NULL, "CLOSED", "CLOSED");
                  newfile.write(data);
                  

                  @

                  Two things, the file paths are blank here for legal reasons( I have tested to make sure they are opening and they are), Second, I realize that by calling newfile.open i am simply recreating a new file that did not exist before( I have not gotten to fixing this yet).
                  Any Suggestions?

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #11

                    You wait for read on the retrieveMedicalIdAccess_Socket and do your actual read on retrieveMedicalIdGateWay_Socket - looks weird to me.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • W Offline
                      W Offline
                      webmaster.skelton
                      wrote on last edited by
                      #12

                      Wow, I can not belive i did that. lol Thanks. That was it. Again i cant belive i did that

                      1 Reply Last reply
                      0
                      • W Offline
                        W Offline
                        webmaster.skelton
                        wrote on last edited by
                        #13

                        The sad part is myself and another developer looked at this several times and managed to skim over that.

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #14

                          Oh, that's a common pattern. You read what you want to be there, not what's actually there. That's why it's so important to have someone else proofread your thesis, books or wiki pages :-)

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          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