サーバー/クライアント システムを自分で設計しています。から拡張してクラスを作成し、接続されたクライアントを処理するようにQTcpServer定義QMap <ClientName, int> socketsします。マップに新しいクライアントsocketsと同じソケットが含まれていない場合、クライアントはサーバーに接続できます。ClientNameそのため、新しいソケットがサーバーに接続すると、クライアントPair <ClientName, SocketDescriptor>を qmap に保存します。これらの説明により、クライアントがサーバーから離れたときに qmap からクライアント記述子を削除する必要がありdisconnectsます。したがって、次のようにスロットを作成void disconnected()して実装します。
void MyServer::disconnected()
{
QTcpSocket* socket = (QTcpSocket*) sender();
ClientType socketType = ClientTypeNone;
foreach(ClientType key, _sockets.keys())
{
if (sockets.value(key) == socket.socketDescriptor())
{
socketType = key;
break;
}
}
if (socketType != ClientTypeNone)
{
sockets.remove(socketType);
}
}
ただし、socket.socketDescriptor-1 ですが、以下のコードで設定しています。
void MyServer::inComingConnection(qintptr socketDescriptor)
{
QTcpSocket* socket = nextPendingConnection();
connect(s, SIGNAL(readyRead()), this, SLOT(readyRead());
connect(s, SIGNAL(disconnected()), this, SLOT(disconnected());
socket->setSocketDescriptor(socketDescriptor);
}
何が間違っていたのですか?