Qt QTcpServer怎么处理客户端断开的消息
用Qt 编写的程序,服务端允许最多五个客户端连接进来,如果其中有一个客户端断开,那么久释放它所占用的内存,以便下次新连接进来的客户端用,那么服务端应该怎么编写disconnected()槽函数,
这是有客户端连接进来时的代码
//服务器端创建套接字
void Server::screateServerSocket ()
{
int nConncetion = 0;
bool bsoketConnection=false;
for ( int i =0;i<MaxCnnection;i++)
{
if (bConnection[i] == true)
{
continue ;
}
else if (bConnection[i] == false)
{
nConncetion =i;
bsoketConnection= true;
break;
}
}
if (bsoketConnection == true)
{
ui.statuslabel->setText(tr("client connection"));
ui.serverSendpushButton->setEnabled (true);
//tcpServer->setMaxPendingConnections(5);
m_clientScoket[nConncetion] = tcpServer->nextPendingConnection();
bConnection[nConncetion] = true ;
connect(m_clientScoket[nConncetion],SIGNAL(disconnect()),this,SLOT(disConnectioned()));//我想知道这个槽函数应该怎么写,来判断是哪个客户端断开连接了,我好释放它
connect(m_clientScoket[nConncetion],SIGNAL(disconnect()),m_clientScoket[nConncetion],SLOT(deleteLater()));
connect(m_clientScoket[nConncetion],SIGNAL(readyRead()),this,SLOT(serverReadMessage()));
QString hostAddress=m_clientScoket[nConncetion]->QAbstractSocket::peerAddress().toString();
int hostPort=m_clientScoket[nConncetion]->QAbstractSocket::peerPort();
ui.servertextBrowser->insertPlainText(tr("hostAddress: %1 (%2)\n").arg(hostAddress).arg(hostPort));
}
else
{
QTcpSocket *clientScoket = tcpServer->nextPendingConnection();
clientScoket->close();
}
}