天津網(wǎng)站建設推廣百度網(wǎng)址大全電腦版舊版本
文章目錄
- 概要:本期主要講解QT中對于TCP協(xié)議通信的實現(xiàn)。
- 一、TCP協(xié)議
- 二、Qt中TCP協(xié)議處理
- 1.QTcpSocket
- 2.QTcpServer
- 三、Qt實現(xiàn)TCP通信
- 1.客戶端
- 2.服務器端
- 結(jié)尾
概要:本期主要講解QT中對于TCP協(xié)議通信的實現(xiàn)。
一、TCP協(xié)議
傳輸控制協(xié)議(TCP,Transmission Control Protocol)是一種面向連接的、可靠的、基于字節(jié)流的傳輸層通信協(xié)議,由IETF的RFC 793 定義。
TCP建立連接前,需要進行三次握手,如下圖所示:
TCP斷開連接前,需要進行四次揮手,如下圖所示:
二、Qt中TCP協(xié)議處理
Qt中提供了QTcpSocket類和QTcpServer類分別用于創(chuàng)建TCP套接字和TCP服務器套接字。
1.QTcpSocket
QTcpSocket類繼承與QAbstractSocket,主要提供了socket套接字的創(chuàng)建、綁定端口、連接服務器等。
2.QTcpServer
QTcpServer類繼承于QSctpServer,主要提供了對于TCP連接信號的響應和監(jiān)聽等。
三、Qt實現(xiàn)TCP通信
1.客戶端
客戶端是發(fā)送端,主要實現(xiàn)與服務器端建立連接、發(fā)送數(shù)據(jù)。步驟如下:
建立TCP套接字 --> 連接服務器 --> 發(fā)送數(shù)據(jù)
下面是客戶端的源碼:
#ifndef TCPCLIENT_H
#define TCPCLIENT_H#include <QObject>
#include <QTcpSocket>
#include <QHostAddress>
#include <QDebug>
#include <QTimer>class TCPClient : QObject
{Q_OBJECT
public:TCPClient();void InitSocket();//初始化Socket套接字void InitTimer();//初始化定時器,定時發(fā)送void ConnectToServer();//連接服務器void SendData();//發(fā)送數(shù)據(jù)private:QTcpSocket *mTcpSocket;//Tcp連接套接字QHostAddress mServerAddress;//服務器IPQTimer *mTimer;//定時器對象};#endif // TCPCLIENT_H
#include "tcpclient.h"TCPClient::TCPClient()
{InitSocket();ConnectToServer();InitTimer();}void TCPClient::InitSocket()
{mTcpSocket = new QTcpSocket;//初始化Tcp連接套接字mServerAddress.setAddress("10.0.0.177");//設置服務器地址
}void TCPClient::InitTimer()
{mTimer = new QTimer;//初始化定時器對象connect(mTimer,&QTimer::timeout,this,[=]{SendData();});mTimer->start(1000);//每隔一秒發(fā)送一次數(shù)據(jù)
}void TCPClient::ConnectToServer()
{quint16 _port = 7777;//設置服務器端口connect(mTcpSocket,&QTcpSocket::connected,this,[=]{qDebug()<< "Connect To Server Successful!"<<endl;});mTcpSocket->connectToHost(mServerAddress,_port,QIODevice::WriteOnly);//連接服務器}void TCPClient::SendData()
{QByteArray _data = "hello";if(-1 != mTcpSocket->write(_data.data(),_data.length())){qDebug()<< "TCP ==> Send data : "<< _data<<endl;}mTcpSocket->flush();
}
2.服務器端
服務器端是接收端,主要實現(xiàn)監(jiān)聽連接信號,建立連接和接收數(shù)據(jù)。步驟如下:
建立監(jiān)聽套接字 --> 連接到客戶端 --> 獲得連接套接字 --> 接收數(shù)據(jù)
下面是服務器端源碼:
#ifndef TCPSEVER_H
#define TCPSEVER_H#include <QObject>
#include <QHostAddress>
#include <QTcpServer>
#include <QTcpSocket>
#include <QDebug>class TCPSever : QObject
{Q_OBJECT
public:TCPSever();void InitServer();//初始化服務器
private:QTcpServer *mTcpServer;//服務器對象QTcpSocket *mTcpSocket;//客戶端套接字QHostAddress mHostAddress;//本地IP地址};#endif // TCPSEVER_H
#include "tcpsever.h"TCPSever::TCPSever()
{InitServer();
}void TCPSever::InitServer()
{mTcpServer = new QTcpServer(this);//初始化監(jiān)聽套接字mTcpSocket = new QTcpSocket;//初始化連接套接字mHostAddress.setAddress("10.0.0.177");//設置監(jiān)聽網(wǎng)卡IPquint16 _port = 7777;//設置監(jiān)聽端口mTcpServer->listen(mHostAddress,_port);//監(jiān)聽指定網(wǎng)卡和端口qDebug()<<"Listen Interface ["<<mHostAddress.toString()<<"] And Port ["<<_port<<"] Successful!"<<endl;connect(mTcpServer,&QTcpServer::newConnection,this,[=]{mTcpSocket = mTcpServer->nextPendingConnection();//獲取連接套接字qDebug()<<"Connect To Client Successful!"<<endl;connect(mTcpSocket,&QTcpSocket::readyRead,this,[=]{//讀取消息QByteArray _data = mTcpSocket->readAll();qDebug()<<"TCP Receive Data : "<<QString::fromLatin1(_data)<<endl;});});}
結(jié)尾
以上就是QT中TCP通信模塊的全部內(nèi)容,然后上面的源碼可以直接編,但是記得去PRO文件中加入network模塊:)