connect(ui->btnListen,&QPushButton::clicked,[this]{ //if Listening,close it if(server->isListening()){ //server->close(); closeServer();//Define function //Recover the status after closed }else{ const QString address_text=ui->editAddress->text(); const QHostAddress address=(address_text=="Any") ?QHostAddress::Any :QHostAddress(address_text); constunsignedshort port=ui->editPort->text().toUShort(); if(server->listen(address,port)){ //handle function }else{ //error function } } }
(3)检测到连接,开始处理:
1 2 3 4 5 6 7 8 9 10 11 12 13
//注释头
connect(server,&QTcpServer::newConnection,this,[this]{ while(server->hasPendingConnections()) { //nextPendingConnection get the next obj QTcpSocket *socket=server->nextPendingConnection(); clientList.append(socket); ui->textRecv->append(QString("(%1:%2) Soket Connected") .arg(socket->peerAddress().toString()) .arg(socket->peerPort())); ui->textRecv->append(recv_text); });