2011-06-13 45 views
0

在下面的代碼中,我可以識別客戶端是否連接並添加了QTableWidget中的行,但我怎麼知道客戶端已斷開連接,所以我可以從同一個表中刪除斷開的客戶端的一行。QTcpServer或QTcpClient(在服務器端)知道,連接的客戶端現在斷開連接

TcpServer::TcpServer(QWidget *parent) :QDialog(parent),ui(new Ui::TcpServer) 
{ 
    ui->setupUi(this); 
    m_coSerSo =new CoServerSocket(this); 
    count=0; 


    connect(m_coSerSo,SIGNAL(newConnection()),this, SLOT(updateConnectionTable())); 
} 

TcpServer::~TcpServer() 
{ 
    delete ui; 
} 
void TcpServer::updateConnectionTable() 
{ 
    int row = ui->tableWidget->rowCount(); 
    ui->tableWidget->setRowCount(row + 1); 
    ui->tableWidget->setItem(row, 0, new QTableWidgetItem(m_coSerSo->getPeerAdd())); 
    ui->tableWidget->setItem(row, 1, 
      new QTableWidgetItem(QDateTime::currentDateTime().toString())); 
} 


CoServerSocket::CoServerSocket(QObject *parent) 
    : QTcpServer(parent) 
{ peerAdd ="good1"; 

} 

void CoServerSocket::incomingConnection(int socketId) 
{ 
    socketClient = new CoClientSocket(this); 
    socketClient->setSocketDescriptor(socketId); 

    peerAdd = socketClient->peerAddress().toString(); 
} 
QString CoServerSocket::getPeerAdd() 
{ 
    return peerAdd; 
} 

回答

0

你內心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<CoisClientSocket*>(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

謝謝,我仍然沒有嘗試映射,但它會幫助我。 – anj 2011-06-14 11:38:20

相關問題