QT编写tcp通信工具(Server端)

本文实例为大家分享了QT编写Server端的tcp通信工具的具体代码,供大家参考,具体内容如下
1.说明
使用qt写一个类似网上常见的网络调试工具。此篇为Server端。Client端在上一篇。
QT编写tcp通信工具(Server端)
文章图片

2.基本流程
新建QTcpServer对象,为其newConnection信号写槽函数。此为新的Client连接信号,在其对应槽函数里使用nextPendingConnection方法获取Client对象,并为Client添加readyRead(读数据),disconnected(断开连接)两个信号写槽函数。
开始监听使用Server的listen方法,停止监听使用Server的close方法。
主动断开某个连接可使用Client对象的disconnectFromHost方法。需要注意的是,这会触发disconnected信号。应小心处理,避免重复删除链表,导致程序崩溃。
3.代码
这是mainwindow.cpp文件

#include "mainwindow.h"#include MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow){ui->setupUi(this); ui->splitter->setStretchFactor(0,2); ui->splitter->setStretchFactor(1,1); tcpServer = new QTcpServer(this); //qWarning()<ledtIp->setText( ip ); //本地IP}ui->labHostName->setText(tr("HostName:")+ QHostInfo::localHostName() ); ui->labHostName->adjustSize(); ui->ledtPort->setText(tr("8080")); ui->btnOpen->setEnabled(true); ui->btnSend->setEnabled(false); iplistMenu = newQMenu(ui->lwdgClientsIp ); iplistMenu->addAction(ui->actiondel); connect( ui->actiondel, SIGNAL(triggered()), this, SLOT(slot_delmenu())); connect(tcpServer, SIGNAL(newConnection()), this, SLOT(slot_newConnectionClient())); } MainWindow::~MainWindow(){delete ui; }//获取本地IP/*QString MainWindow::getLocalip(){QHostInfo info = QHostInfo::fromName( QHostInfo::localHostName() ); foreach(QHostAddress addr,info.addresses()){if(addr.protocol()==QAbstractSocket::IPv4Protocol){returnaddr.toString(); }}return ""; }*/QString MainWindow::getLocalip(){QList list = QNetworkInterface::allAddresses(); foreach (QHostAddress address, list){if( address.protocol() == QAbstractSocket::IPv4Protocol){return address.toString(); }}return ""; } //处理来自新的client连接void MainWindow::slot_newConnectionClient(){while (tcpServer->hasPendingConnections()){QTcpSocket *client = tcpServer->nextPendingConnection(); tcpClients.append(client); ui->lwdgClientsIp->addItem( tr("%1:%2").arg(client->peerAddress().toString()).arg(client->peerPort()) ); connect(client, SIGNAL(readyRead()), this, SLOT(slot_readData())); connect(client, SIGNAL(disconnected()), this, SLOT(slot_disconnectedClient())); }} //接收数据void MainWindow::slot_readData(){QTcpSocket *obj = (QTcpSocket*)sender(); QByteArray buf = obj->readAll(); if(buf.isEmpty())return; QString ipc; ipc = tr("[%1:%2]>").arg(obj->peerAddress().toString()).arg(obj->peerPort()); ui->teRecive->appendPlainText(ipc); ui->teRecive->appendPlainText(buf); } //断开连接处理voidMainWindow::slot_disconnectedClient(){if( !tcpClients.isEmpty() ){QTcpSocket *obj = (QTcpSocket*)sender(); QListWidgetItem *item =ui->lwdgClientsIp->findItems(tr("%1:%2")\.arg( obj->peerAddress().toString())\.arg( obj->peerPort()),Qt::MatchContains|Qt::MatchEndsWith).at(0); ui->lwdgClientsIp->removeItemWidget( item ); delete item; obj->close(); tcpClients.removeOne(obj); }} //打开监听与停止void MainWindow::on_btnOpen_clicked(){if(ui->btnOpen->text() == "停止")//主动停止监听{ if( !tcpClients.isEmpty() )for(int i=0; idisconnectFromHost(); //会触发disconnected信号}tcpClients.clear(); tcpServer->close(); ui->lwdgClientsIp->clear(); ui->btnOpen->setText("监听"); ui->btnSend->setEnabled(false); }else{ //打开监听if(ui->ledtPort->text().toInt() == 0 ){QMessageBox::about(this,tr("提示"),tr("请输入端口号。") ) ; }elseif( tcpServer->listen( QHostAddress::AnyIPv4, ui->ledtPort->text().toInt() ) ){ui->btnOpen->setText("停止"); ui->btnSend->setEnabled(true); }}} //发送数据,UTF8模式void MainWindow::on_btnSend_clicked(){if( ui->lwdgClientsIp->selectedItems().length() >0 ){foreach( QListWidgetItem* item ,ui->lwdgClientsIp->selectedItems() ){QString clientIP = item->text().split(":")[0]; int clientPort = item->text().split(":")[1].toInt(); for(int i=0; ipeerAddress().toString()==clientIP && tcpClients[i]->peerPort()==clientPort){tcpClients[i]->write(ui->teSend->toPlainText().toUtf8() ); break; }}}}}//“删除”菜单,停止某个Clientvoid MainWindow:: slot_delmenu(){if(lwdgitem != NULL ){if( !tcpClients.isEmpty() ){QString clientIP = lwdgitem->text().split(":")[0]; int clientPort = lwdgitem->text().split(":")[1].toInt(); for(int i=0; ipeerAddress().toString()==clientIP && tcpClients[i]->peerPort()==clientPort){tcpClients[i]->disconnectFromHost(); break; }}}}}//弹出菜单void MainWindow::on_lwdgClientsIp_customContextMenuRequested(const QPoint &pos){lwdgitem=ui->lwdgClientsIp->itemAt(pos ) ; //判断是否在项目上if(lwdgitem != NULL ){iplistMenu->exec(QCursor::pos()); }}

【QT编写tcp通信工具(Server端)】以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读