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();

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在QT中,可以通过以下步骤来判断TCP服务器是否断开连接: 1. 创建一个QTcpServer对象,并监听指定的口。 2. 在槽函数`acceptConnection()`中,使用`nextPendingConnection()`函数接受客户连接,并将返回的QTcpSocket对象保存起来。 3. 在槽函数`updateServerRead()`中,使用`bytesAvailable()`函数检查是否有数据可读。如果返回值为0,表示客户已经断开连接。 4. 在槽函数`MSGError(QAbstractSocket::SocketError)`中,使用`error()`函数检查是否有错误发生。如果返回值为QAbstractSocket::RemoteHostClosedError,表示客户已经断开连接。 下面是一个示例代码: ```cpp // 头文件部分 private: Ui::MainWindow *ui; QTcpServer m_tcpServer; QTcpSocket *m_tcpServerConnection; public slots: void start_monitor(); void acceptConnection(); void updateServerRead(); void MSGError(QAbstractSocket::SocketError); // 实部分 void MainWindow::start_monitor() { // 监听指定口 if (!m_tcpServer.listen(QHostAddress::Any, 1234)) { qDebug() << "Failed to start server:" << m_tcpServer.errorString(); return; } qDebug() << "Server started!"; } void MainWindow::acceptConnection() { // 接受客户连接 m_tcpServerConnection = m_tcpServer.nextPendingConnection(); connect(m_tcpServerConnection, SIGNAL(readyRead()), this, SLOT(updateServerRead())); connect(m_tcpServerConnection, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(MSGError(QAbstractSocket::SocketError))); qDebug() << "Client connected!"; } void MainWindow::updateServerRead() { // 检查是否有数据可读 if (m_tcpServerConnection->bytesAvailable() == 0) { qDebug() << "Client disconnected!"; } } void MainWindow::MSGError(QAbstractSocket::SocketError) { // 检查是否有错误发生 if (m_tcpServerConnection->error() == QAbstractSocket::RemoteHostClosedError) { qDebug() << "Client disconnected!"; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值