亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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++內(nèi)存泄漏及檢測工具詳解

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

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

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

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

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

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

    OpenGL畫bezier曲線

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

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

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

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

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

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

    所謂的C語言多文件編程就是,將代碼實現(xiàn)模塊化。比如說一個項目的一項功能放在一個一個文件里,然后將實現(xiàn)這個功能的函數(shù)放在一個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)實現(xiàn)方式,借鑒了值語義,保留了繼承,在單繼承的適用范圍內(nèi),程序和程序員都能從中受益。這篇文章主要介紹了C++值多態(tài)中的傳統(tǒng)多態(tài)與類型擦除,需要的朋友可以參考下
    2020-04-04
  • C++ deque容器的用法詳解

    C++ deque容器的用法詳解

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

    C++Node類Cartographer開始軌跡的處理深度詳解

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

最新評論