欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

1.說(shuō)明

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

2.基本流程

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

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

主動(dòng)斷開(kāi)某個(gè)連接可使用Client對(duì)象的disconnectFromHost方法。需要注意的是,這會(huì)觸發(fā)disconnected信號(hào)。應(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("無(wú)本地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 "";
}
?
//處理來(lái)自新的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);
}
?
//斷開(kāi)連接處理
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);
? ? }
}
?
//打開(kāi)監(jiān)聽(tīng)與停止
void MainWindow::on_btnOpen_clicked()
{
? ? if(ui->btnOpen->text() == "停止")//主動(dòng)停止監(jiān)聽(tīng)
? ? {
?
? ? ? ? if( !tcpClients.isEmpty() )
? ? ? ? for(int i=0; i<tcpClients.length(); i++)//斷開(kāi)所有連接
? ? ? ? {
? ? ? ? ? ? tcpClients[i]->disconnectFromHost(); //會(huì)觸發(fā)disconnected信號(hào)
? ? ? ? }
? ? ? ? tcpClients.clear();
? ? ? ? tcpServer->close();
? ? ? ? ui->lwdgClientsIp->clear();
? ? ? ? ui->btnOpen->setText("監(jiān)聽(tīng)");
? ? ? ? ui->btnSend->setEnabled(false);
? ? }else{ //打開(kāi)監(jiān)聽(tīng)
? ? ? ? if( ?ui->ledtPort->text().toInt() == 0 )
? ? ? ? {
? ? ? ? ? ? QMessageBox::about(this,tr("提示"),tr("請(qǐng)輸入端口號(hào)。") ) ;
? ? ? ? }
? ? ? ? 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;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
//“刪除”菜單,停止某個(gè)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 ) ;//判斷是否在項(xiàng)目上
? ? if( ?lwdgitem != NULL )
? ? {
? ? ? ? iplistMenu->exec(QCursor::pos());
? ? }
}

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

相關(guān)文章

  • C++內(nèi)存泄漏及檢測(cè)工具詳解

    C++內(nèi)存泄漏及檢測(cè)工具詳解

    最簡(jiǎn)單的方法當(dāng)然是借助于專業(yè)的檢測(cè)工具,比較有名如BoundsCheck,功能非常強(qiáng)大,相信做C++開(kāi)發(fā)的人都離不開(kāi)它。此外就是不使用任何工具,而是自己來(lái)實(shí)現(xiàn)對(duì)內(nèi)存泄露的監(jiān)控
    2013-10-10
  • C語(yǔ)言經(jīng)典順序表真題演練講解

    C語(yǔ)言經(jīng)典順序表真題演練講解

    程序中經(jīng)常需要將一組數(shù)據(jù)元素作為整體管理和使用,需要?jiǎng)?chuàng)建這種元素組,用變量記錄它們,傳進(jìn)傳出函數(shù)等。一組數(shù)據(jù)中包含的元素個(gè)數(shù)可能發(fā)生變化,順序表則是將元素順序地存放在一塊連續(xù)的存儲(chǔ)區(qū)里,元素間的順序關(guān)系由它們的存儲(chǔ)順序自然表示
    2022-04-04
  • Opencv實(shí)現(xiàn)讀取攝像頭和視頻數(shù)據(jù)

    Opencv實(shí)現(xiàn)讀取攝像頭和視頻數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了Opencv實(shí)現(xiàn)讀取攝像頭和視頻數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • OpenGL畫bezier曲線

    OpenGL畫bezier曲線

    這篇文章主要為大家詳細(xì)介紹了OpenGL畫bezier曲線,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • Linux網(wǎng)絡(luò)編程之socket文件傳輸示例

    Linux網(wǎng)絡(luò)編程之socket文件傳輸示例

    這篇文章主要介紹了Linux網(wǎng)絡(luò)編程之socket文件傳輸示例,對(duì)于基于Linux平臺(tái)的C程序員來(lái)說(shuō)有一定的借鑒價(jià)值,需要的朋友可以參考下
    2014-08-08
  • C語(yǔ)言深入探索數(shù)據(jù)類型的存儲(chǔ)

    C語(yǔ)言深入探索數(shù)據(jù)類型的存儲(chǔ)

    使用編程語(yǔ)言進(jìn)行編程時(shí),需要用到各種變量來(lái)存儲(chǔ)各種信息。變量保留的是它所存儲(chǔ)的值的內(nèi)存位置。這意味著,當(dāng)您創(chuàng)建一個(gè)變量時(shí),就會(huì)在內(nèi)存中保留一些空間。您可能需要存儲(chǔ)各種數(shù)據(jù)類型的信息,操作系統(tǒng)會(huì)根據(jù)變量的數(shù)據(jù)類型,來(lái)分配內(nèi)存和決定在保留內(nèi)存中存儲(chǔ)什么
    2022-07-07
  • C語(yǔ)言詳細(xì)分析講解多文件的程序設(shè)計(jì)

    C語(yǔ)言詳細(xì)分析講解多文件的程序設(shè)計(jì)

    所謂的C語(yǔ)言多文件編程就是,將代碼實(shí)現(xiàn)模塊化。比如說(shuō)一個(gè)項(xiàng)目的一項(xiàng)功能放在一個(gè)一個(gè)文件里,然后將實(shí)現(xiàn)這個(gè)功能的函數(shù)放在一個(gè)c文件<BR>
    2022-04-04
  • 詳解C++值多態(tài)中的傳統(tǒng)多態(tài)與類型擦除

    詳解C++值多態(tài)中的傳統(tǒng)多態(tài)與類型擦除

    值多態(tài)是一種介于傳統(tǒng)多態(tài)與類型擦除之間的多態(tài)實(shí)現(xiàn)方式,借鑒了值語(yǔ)義,保留了繼承,在單繼承的適用范圍內(nèi),程序和程序員都能從中受益。這篇文章主要介紹了C++值多態(tài)中的傳統(tǒng)多態(tài)與類型擦除,需要的朋友可以參考下
    2020-04-04
  • C++ deque容器的用法詳解

    C++ deque容器的用法詳解

    在處理一些數(shù)組的事情,所以隨手保留一下Deque容器的使用方法很有必要,接下來(lái)通過(guò)本文給大家重點(diǎn)介紹C++ deque容器的用法及deque和vector的區(qū)別講解,感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • C++Node類Cartographer開(kāi)始軌跡的處理深度詳解

    C++Node類Cartographer開(kāi)始軌跡的處理深度詳解

    這篇文章主要介紹了C++Node類Cartographer開(kāi)始軌跡的處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2023-03-03

最新評(píng)論