最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

QT編寫tcp通信工具(Server端)

 更新時間:2022年08月19日 10:39:13   作者:林子xxx  
這篇文章主要為大家詳細介紹了QT編寫tcp通信工具,一個類似網(wǎng)上常見的網(wǎng)絡(luò)調(diào)試工具,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了QT編寫Server端的tcp通信工具的具體代碼,供大家參考,具體內(nèi)容如下

1.說明

使用qt寫一個類似網(wǎng)上常見的網(wǎng)絡(luò)調(diào)試工具。此篇為Server端。Client端在上一篇。

2.基本流程

新建QTcpServer對象,為其newConnection信號寫槽函數(shù)。此為新的Client連接信號,在其對應(yīng)槽函數(shù)里使用nextPendingConnection方法獲取Client對象,并為Client添加readyRead(讀數(shù)據(jù)),disconnected(斷開連接)兩個信號寫槽函數(shù)。

開始監(jiān)聽使用Server的listen方法,停止監(jiān)聽使用Server的close方法。

主動斷開某個連接可使用Client對象的disconnectFromHost方法。需要注意的是,這會觸發(fā)disconnected信號。應(yīng)小心處理,避免重復(fù)刪除鏈表,導(dǎo)致程序崩潰。

3.代碼

這是mainwindow.cpp文件

#include "mainwindow.h"
#include <QApplication>
?
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()<<QNetworkInterface().allAddresses();
? ? QString ip=getLocalip();
? ? if( ip.isEmpty() )
? ? {
? ? ? ? QMessageBox::about(this,tr("提示"),tr("無本地IP。") ) ;
? ? }
? ? else{
?
? ? ? ? ui->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 = new ?QMenu( ?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)
? ? ? ? {
? ? ? ? ? ? return ?addr.toString();
? ? ? ? }
? ? }
? ? return "";
}
*/
QString MainWindow::getLocalip()
{
? ? QList<QHostAddress> 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()));
? ? }
}
?
//接收數(shù)據(jù)
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);
}
?
//斷開連接處理
void ?MainWindow::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);
? ? }
}
?
//打開監(jiān)聽與停止
void MainWindow::on_btnOpen_clicked()
{
? ? if(ui->btnOpen->text() == "停止")//主動停止監(jiān)聽
? ? {
?
? ? ? ? if( !tcpClients.isEmpty() )
? ? ? ? for(int i=0; i<tcpClients.length(); i++)//斷開所有連接
? ? ? ? {
? ? ? ? ? ? tcpClients[i]->disconnectFromHost(); //會觸發(fā)disconnected信號
? ? ? ? }
? ? ? ? tcpClients.clear();
? ? ? ? tcpServer->close();
? ? ? ? ui->lwdgClientsIp->clear();
? ? ? ? ui->btnOpen->setText("監(jiān)聽");
? ? ? ? ui->btnSend->setEnabled(false);
? ? }else{ //打開監(jiān)聽
? ? ? ? if( ?ui->ledtPort->text().toInt() == 0 )
? ? ? ? {
? ? ? ? ? ? QMessageBox::about(this,tr("提示"),tr("請輸入端口號。") ) ;
? ? ? ? }
? ? ? ? else
? ? ? ? if( tcpServer->listen( QHostAddress::AnyIPv4 ?, ui->ledtPort->text().toInt() ) )
? ? ? ? {
? ? ? ? ? ? ui->btnOpen->setText("停止");
? ? ? ? ? ? ui->btnSend->setEnabled(true);
? ? ? ? }
? ? }
}
?
//發(fā)送數(shù)據(jù),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; i<tcpClients.length(); i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(tcpClients[i]->peerAddress().toString()==clientIP && tcpClients[i]->peerPort()==clientPort)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? tcpClients[i]->write(ui->teSend->toPlainText().toUtf8() );
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
//“刪除”菜單,停止某個Client
void 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; i<tcpClients.length(); i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(tcpClients[i]->peerAddress().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());
? ? }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 貪吃蛇游戲C++命令行版實例代碼

    貪吃蛇游戲C++命令行版實例代碼

    這篇文章主要介紹了貪吃蛇游戲C++命令行版實例代碼,包含了常見的循環(huán)語句及相關(guān)游戲規(guī)則的判定方法,有助于更好的理解游戲設(shè)計原理,需要的朋友可以參考下
    2014-09-09
  • SQL Server中的數(shù)據(jù)復(fù)制到的Access中的函數(shù)

    SQL Server中的數(shù)據(jù)復(fù)制到的Access中的函數(shù)

    SQL Server中的數(shù)據(jù)復(fù)制到的Access中,表的結(jié)構(gòu)相同 不要提用openrowset,因為Access文件和SQL Server不在一臺機器上
    2008-11-11
  • 淺談C++流庫的基本結(jié)構(gòu)

    淺談C++流庫的基本結(jié)構(gòu)

    本文主要介紹了淺談C++流庫的基本結(jié)構(gòu),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 淺析C++?atomic?和?memory?ordering

    淺析C++?atomic?和?memory?ordering

    這篇文章主要介紹了C++?atomic?和?memory?ordering的相關(guān)知識,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • 超級詳細講解C++中的多態(tài)

    超級詳細講解C++中的多態(tài)

    多態(tài)是在不同繼承關(guān)系的類對象,去調(diào)同一函數(shù),產(chǎn)生了不同的行為,下面這篇文章主要給大家介紹了關(guān)于C++中多態(tài)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-05-05
  • C++ 中的this指針詳解及實例

    C++ 中的this指針詳解及實例

    這篇文章主要介紹了C++ 中的this指針詳解及實例的相關(guān)資料,this指針是類的一個自動生成、自動隱蔽的私有成員,它存在于類的非靜態(tài)成員中,指向被調(diào)用函數(shù)所在的對象。需要的朋友可以參考下
    2017-07-07
  • 深入淺析C語言中堆棧和隊列

    深入淺析C語言中堆棧和隊列

    這篇文章主要介紹了深入淺析C語言中堆棧和隊列的相關(guān)資料,需要的朋友可以參考下
    2016-06-06
  • C語言實現(xiàn)職工管理系統(tǒng)

    C語言實現(xiàn)職工管理系統(tǒng)

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)職工管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • 深入理解QT多線程編程

    深入理解QT多線程編程

    本文主要介紹了QT多線程編程的深入理解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • C++實現(xiàn)俄羅斯方塊游戲

    C++實現(xiàn)俄羅斯方塊游戲

    這篇文章主要為大家詳細介紹了C++實現(xiàn)俄羅斯方塊游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09

最新評論

唐山市| 姜堰市| 莫力| 肇东市| 宣威市| 邛崃市| 沐川县| 余庆县| 雅安市| 莱州市| 九台市| 平度市| 镇江市| 阜康市| 古丈县| 石狮市| 阿拉善左旗| 保定市| 鄂尔多斯市| 永康市| 高雄县| 叶城县| 万宁市| 义马市| 宁都县| 贡嘎县| 梅河口市| 瑞金市| 临夏县| 靖江市| 乐山市| 北宁市| 葵青区| 奇台县| 大庆市| 增城市| 禄丰县| 秀山| 日土县| 景谷| 南平市|