Closing one socket closes multiple connections
-
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 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(path); QFile newfile(""); if(newfile.open((QIODevice::WriteOnly | QIODevice::Text))) { 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? -
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.