正常关闭QTcpsocket

myClient->disconnectFromHost();
Qt的例子里面有这段话:
which will close the connection after QTcpSocket has finished writing the fortune to the network. Because QTcpSocket works asynchronously, the data will be written after this function returns, and control goes back to Qt's event loop. The socket will then close。
也就是说,和服务器连接的断开会在发送完数据之后进行。
如果我们加了这样一个槽函数:
connect(myClient, SIGNAL(disconnected()),myClient, SLOT(deleteLater()));

就能够避免出现内存泄漏。



 QTcpSocket *myClient=tcpServer->nextPendingConnection();

    //我们获取已经建立的连接的子套接字

   connect(myClient,SIGNAL(disconnected()),myClient,SLOT(deleteLater()));

   myClient->write(block);

   myClient->disconnectFromHost();


你可能感兴趣的:(正常关闭QTcpsocket)