日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Qt實現TCP網絡編程_C 語言

作者:一滴涌入大海的雨滴 ? 更新時間: 2022-10-16 編程語言

本文實例為大家分享了Qt實現TCP網絡編程的具體代碼,供大家參考,具體內容如下

1.Qt中的TCP客戶端編程

Qt中的TCP客戶端編程:

對于Qt編程而言,網絡只是數據傳輸的通道
Qt提供了QTcpSocket類(封裝了TCP協議細節)
將QTcpSocket的對象當做黑盒使用,進行數據收發

QTcpSocket的使用方式:

1.連接服務端主機(connectToHost())
2.發送數據/接受數據(write()/read())
3.關閉連接(close())

QTcpSocket的注意事項:

默認情況下,QTcpSocket使用異步編程的方式:
操作完成后立即返回
通過發送信號的方式返回操作結果
QTcpSocket提供了輔助函數,可完成同步編程的方式
waitForConnected()/waitForDisconnected()
waitForBytesWritten()/waitForReadyread()

QTcpSocket的同步編程:

編程實驗:同步編程

#include <QCoreApplication>
#include <QTcpSocket>
#include <QDebug>
#include <QThread>

void SyncClientDemo()
{
? ? QTcpSocket client;
? ? char buf[256] = {0};
? ? client.connectToHost("127.0.0.1",8080);

? ? qDebug() << "Connected:" << client.waitForConnected();
? ? qDebug() << "Send Bytes:" << client.write("CKY");
? ? qDebug() << "Send Status:" << client.waitForBytesWritten();
? ? qDebug() << "Data Avilable:" << client.waitForReadyRead();
? ? qDebug() << "Received Bytes:" << client.read(buf, sizeof(buf));
? ? qDebug() << "Received Data:" << buf;

? ? QThread::sleep(5000);

? ? client.close();

? ? client.waitForDisconnected();

}

int main(int argc, char *argv[])
{
? ? QCoreApplication a(argc, argv);

? ? SyncClientDemo();

? ? return a.exec();
}

QTcpSocket的異步編程:

QTcpSocket對象通過發送信號的方式返回操作結果
可以在程序中將對應的信號連接到槽函數,獲取結果
GUI應用程序中通常使用QTcpSocket的異步方式

QTcpSocket中的關鍵信號:

connected():成功連接遠端主機
disconnected():遠端主機斷開連接
readyRead():遠程數據到達本機
bytesWritten(qint64):數據成功發送至系統(OS)

編程實驗:QTcpSocket異步編程

#include "clientdemo.h"
#include <QDebug>
#include <QHostAddress>

ClientDemo::ClientDemo(QObject* parent) : QObject(parent)
{
? ? connect(&m_client, SIGNAL(connected()), this, SLOT(onConnected()));
? ? connect(&m_client, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
? ? connect(&m_client, SIGNAL(readyRead()), this, SLOT(onDataReady()));
? ? connect(&m_client, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));
}


void ClientDemo::onConnected()
{
? ? qDebug() << "onConnected()";
? ? qDebug() << "Local Address:" << m_client.localAddress();
? ? qDebug() << "Loacl Port:" << m_client.localPort();
}
void ClientDemo::onDisconnected()
{
? ? qDebug() << "onDiecennected()";
}
void ClientDemo::onDataReady()
{
? ? char buf[256] = {0};

? ? qDebug() << "onDataReady:" << m_client.read(buf, sizeof(buf));
? ? qDebug() << "Data:" << buf;
}
void ClientDemo::onBytesWritten(qint64 bytes)
{
? ? qDebug() << "onBytesWritten" << bytes;
}
void ClientDemo::connectTo(QString ip, int port)
{
? ? m_client.connectToHost(ip, port);
}
qint64 ClientDemo::send(const char* data, int len)
{
? ? return m_client.write(data, len);
}
qint64 ClientDemo::available()
{
? ? return m_client.bytesAvailable();
}
void ClientDemo::close()
{
? ? m_client.close();
}

1.Qt中的TCP服務端編程

網絡中的服務端:
服務端是為客戶端服務的,服務的內容諸如向客戶端提供資源,保存客戶端數據,為客戶端提供功能接口,等
Client/Server軟件架構簡介

特點;
服務端被動接受連接(服務端無法主動連接客戶端)
服務端必須公開網絡地址(容易受到攻擊)
在職責上:
客戶端傾向于處理用于交互及體驗(GUI)
服務端傾向于用戶數據的組織和存儲(數據處理)

B/S網絡結構是什么?
Browser/Server軟件架構簡介
B/S是一種特殊的C/S網絡架構
B/S中的客戶端統一使用瀏覽器(Browser)
B/S中的客戶端GUI通常采用HTML進行開發
B/S中的客戶端與服務端通常采用http協議進行通信

Qt中的TCP服務端編程:

Qt提供了QTcpServer類
將QTcpServer的對象當做黑盒使用,進行連接監聽
每一個連接生成一個QTcpSocket對象進行通信

QTcpServer的使用方式:

監聽本機地址的端口(listen())
通過信號通知客戶端連接(newConnection())
獲取QTcpSocket通信對象(nextPendingConnection())
停止監聽(close())

QTcpServer的注意事項:

用于處理客戶端連接,不進行具體通信
監聽的端口只用于響應連接請求
監聽到連接后,生成QTcpSocket對象與客戶端通信

Client/Server交互流程:

編程實驗:QServerSocket編程

#include "serverdemo.h"
#include "QHostAddress"
#include <QDebug>
#include <QTcpServer>
#include <QObjectList>

ServerDemo::ServerDemo(QObject* parent) : QObject(parent)
{
? ? connect(&m_server, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
}

void ServerDemo::onNewConnection()
{
? ? qDebug() << "onNewConnection";
? ? QTcpSocket* tcp = m_server.nextPendingConnection();
? ? connect(tcp, SIGNAL(connected()), this, SLOT(onConnected()));
? ? connect(tcp, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
? ? connect(tcp, SIGNAL(readyRead()), this, SLOT(onDataReady()));
? ? connect(tcp, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));
}

bool ServerDemo::start(int port)
{
? ? bool ret = true;

? ? if(!m_server.isListening())
? ? {
? ? ? ? ret = m_server.listen(QHostAddress("127.0.0.1", port));
? ? }

? ? return ret;
}

void ServerDemo::stop()
{
? ? if(m_server.isListening())
? ? {
? ? ? ? m_server.close();
? ? }
}

void ServerDemo::onConnected()
{
? ? QTcpServer* tcp = dynamic_cast<QTcpServer*>(sender());

? ? if(tcp != NULL)
? ? {
? ? ? ? qDebug() << "onConnected()";
? ? ? ? qDebug() << "Local Address:" << tcp->localAddress();
? ? ? ? qDebug() << "Loacl Port:" << tcp->localPort();
? ? }

}
void ServerDemo::onDisconnected()
{
? ? qDebug() << "onDiecennected()";
}
void ServerDemo::onDataReady()
{
? ? QTcpServer* tcp = dynamic_cast<QTcpServer*>(sender());
? ? char buf[256] = {0};

? ? if(tcp != NULL)
? ? {
? ? ? ? qDebug() << "onDataReady:" << tcp->read(buf, sizeof(buf));
? ? ? ? qDebug() << "Data:" << buf;
? ? }
}
void ServerDemo::onBytesWritten(qint64 bytes)
{
? ? qDebug() << "onBytesWritten" << bytes;
}

ServerDemo::~ServerDemo()
{
? ? const QObjectList& list = m_server.children();
? ? for(int i = 0; i < list.length(), i++)
? ? {
? ? ? ? QTcpSocket* tcp = dynamic_cast<QTcpSocket*>(list[i]);
? ? ? ? if(tcp != NULL)
? ? ? ? {
? ? ? ? ? ? tcp->close();
? ? ? ? }
? ? }
}

原文鏈接:https://blog.csdn.net/weixin_46571142/article/details/123029759

欄目分類
最近更新