1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > tcpserver检测断开qt_QTcpServer或QTcpClient(在服务器端)知道 已连接的客户端现已断开连接...

tcpserver检测断开qt_QTcpServer或QTcpClient(在服务器端)知道 已连接的客户端现已断开连接...

时间:2019-07-26 18:11:23

相关推荐

tcpserver检测断开qt_QTcpServer或QTcpClient(在服务器端)知道 已连接的客户端现已断开连接...

您在incomingConnection(int)内创建的套接字具有disconnected()信号。使用QSignalMapper确定断开连接的套接字并更新表视图。快速而脏的代码,可能很有帮助,当然还有很多语法错误:

TcpServer::TcpServer(QWidget *parent) :

QDialog(parent),

ui(new Ui::TcpServer)

{

ui->setupUi(this);

m_coisSerSo = new CoisServerSocket(this);

count = 0;

// The mapper forwards the signal from the client socket,

// adding the socket itself as an argument.

this->mapper = new QSignalMapper(this);

connect(this->mapper, SIGNAL(mapped(QObject*)),

this, SLOT(clientDisconnected(QObject*)));

connect(m_coisSerSo,SIGNAL(newConnection()),this, SLOT(updateConnectionTable()));

}

void CoisServerSocket::incomingConnection(int socketId)

{

socketClient = new CoisClientSocket(this);

socketClient->setSocketDescriptor(socketId);

// Map the socket so that we can receive its disconnection

// notification. When the socket emits the "disconnected()"

// signal, the mapper will emit "mapped(QObject*)" and will

// pass the socket as the argument.

this->mapper->setMapping(socketClient, socketClient);

connect(socketClient, SIGNAL(disconnected()), this->mapper, SLOT(map()));

peerAdd = socketClient->peerAddress().toString();

}

void CoisServerSocket::clientDisconnected(QObject* object)

{

if (CoisClientSocket* client = qobject_cast(object))

{

// Unmap the socket.

this->mapper->removeMappings(client);

// Handle disconnection here.

...

// A consequence of mixing a TCP server and a dialog in the

// same class is that passing "this" as an argument to the

// socket constructor doesn't help much with memory management.

// They will stay around as long as the dialog lives. Delete

// them once they aren't needed anymore.

client->deleteLater();

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。