Closing one socket closes multiple connections



  • This may a stupid question. But I am trying to write a client side app where there are up to six possible sockets connecting to various servers. Now if i want to connect two sockets to the same server at the same time, and write data to that server the sockets close before the data write is complete. Let me elaborate. One socket connects, sends up xml, when it is complete it disconnects from host, after that another function is called to upload a different xml, but it only writes a part of the xml. I know the problem lies with calling socket.disconnectFromHost(the first socket). But there is no reason it should disconnect the second socket while it is still transferring data.( and yes they are completely different sockets). Is there something I am missing or is that normal behavior?



  • It's not normal behaviour. Do you have a "testcase":http://homepage1.nifty.com/algafield/sscce.html to post?



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



  • 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]



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



  • 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)



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



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



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



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



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



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



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



  • 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 :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.