一、QT如果要進行網(wǎng)絡(luò)編程首先需要在.pro中添加如下代碼:
QT += network
二、QT下的TCP通信過程
Qt中提供的所有的Socket類都是非阻賽的
Qt中常用的用于socket通信的套接字類
????????QTcpServer
用于TCP/IP通信,作為服務(wù)器端套接字使用
????????QTcpSocket
用于TCP/IP通信,作為客戶端套接字使用。
????????QUdpSocket
用于UDP通信,服務(wù)器,客戶端均使用此套接字
三、描述Qt下Tcp通信的整個流程?
服務(wù)器端:
1. 創(chuàng)建用于監(jiān)聽的套接字?
//監(jiān)聽套接字
tcpServer = new QTcpServer(this);
2. 給套接字設(shè)置監(jiān)聽?
tcpServer->listen(QHostAddress::Any,8888);
setWindowTitle("服務(wù)器:8888");
3. 如果有連接到來, 監(jiān)聽的套接字會發(fā)出信號newConnected?
4. 接收連接, 通過nextPendingConnection()函數(shù), 返回一個QTcpSocket類型的套接字對象(用于通信)?
connect(tcpServer,&QTcpServer::newConnection,[=](){
//取出建立好連接的套接字
tcpSocket = tcpServer->nextPendingConnection();
});
5. 使用用于通信的套接字對象通信?
(1). 發(fā)送數(shù)據(jù): write?
if(NULL == tcpSocket)
{
return;
}
//獲取編輯區(qū)內(nèi)容
QString str = ui->textEditWrite->toPlainText();
//給對方發(fā)送數(shù)據(jù),使用套接字是tcpSocket
tcpSocket->write(str.toUtf8().data());
(2). 接收數(shù)據(jù): readAll/read??
connect(tcpSocket,&QTcpSocket::readyRead,[=](){
//從通信套接字中取出內(nèi)容
QByteArray array = tcpSocket->readAll();
array = "客戶端;" + array;
ui->textEditRead->append(array);
});
客戶端:
1. 創(chuàng)建用于通信的套接字?
tcpSocket = new QTcpSocket(this);
setWindowTitle("客戶端");
2. 連接服務(wù)器: connectToHost?
//獲取服務(wù)器IP和端口
QString ip = ui->lineEditIP->text();
qint16 port = ui->lineEditPort->text().toInt();
//主動與服務(wù)器建立連接
tcpSocket->connectToHost(QHostAddress(ip),port);
connect(tcpSocket,&QTcpSocket::connected,[=](){
ui->textEditRead->setText("成功和服務(wù)器建立好連接");
});
3. 連接成功與服務(wù)器通信?
(1).發(fā)送數(shù)據(jù): write?
//獲取編輯框內(nèi)容
QString str = ui->textEditWrite->toPlainText();
//發(fā)送數(shù)據(jù)
tcpSocket->write(str.toUtf8().data());
(2).接收數(shù)據(jù): readAll/read
connect(tcpSocket,&QTcpSocket::readyRead,[=](){
//獲取對方發(fā)送的內(nèi)容
QByteArray array = tcpSocket->readAll();
//追加到編輯區(qū)中
array = "服務(wù)端:" + array;
ui->textEditRead->append(array);
});
四、完整代碼
服務(wù)端:
文章來源:http://www.zghlxwxcb.cn/news/detail-497047.html
?setverwidget.h
#ifndef SERVERWIDGET_H
#define SERVERWIDGET_H
#include <QWidget>
#include <QTcpServer> //監(jiān)聽套接字
#include <QTcpSocket> //通信套接字
namespace Ui {
class ServerWidget;
}
class ServerWidget : public QWidget
{
Q_OBJECT
public:
explicit ServerWidget(QWidget *parent = 0);
~ServerWidget();
private slots:
void on_buttonSend_clicked();
void on_pushButton_2_clicked();
private:
Ui::ServerWidget *ui;
QTcpServer *tcpServer;//監(jiān)聽套接字
QTcpSocket *tcpSocket;//通信套接字
};
#endif // SERVERWIDGET_H
serverwidget.cpp
#include "serverwidget.h"
#include "ui_serverwidget.h"
ServerWidget::ServerWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ServerWidget)
{
ui->setupUi(this);
tcpServer = NULL;
tcpSocket = NULL;
//監(jiān)聽套接字
tcpServer = new QTcpServer(this);
tcpServer->listen(QHostAddress::Any,8888);
setWindowTitle("服務(wù)器:8888");
connect(tcpServer,&QTcpServer::newConnection,[=](){
//取出建立好連接的套接字
tcpSocket = tcpServer->nextPendingConnection();
//獲取對方的IP和端口
QString ip = tcpSocket->peerAddress().toString();
qint16 port = tcpSocket->peerPort();
QString temp = QString("[%1:%2]:連接成功").arg(ip).arg(port);
ui->textEditRead->setText(temp);
connect(tcpSocket,&QTcpSocket::readyRead,[=](){
//從通信套接字中取出內(nèi)容
QByteArray array = tcpSocket->readAll();
array = "客戶端;" + array;
ui->textEditRead->append(array);
});
});
}
ServerWidget::~ServerWidget()
{
delete ui;
}
void ServerWidget::on_buttonSend_clicked()
{
if(NULL == tcpSocket)
{
return;
}
//獲取編輯區(qū)內(nèi)容
QString str = ui->textEditWrite->toPlainText();
//給對方發(fā)送數(shù)據(jù),使用套接字是tcpSocket
tcpSocket->write(str.toUtf8().data());
ui->textEditWrite->setText("");
}
void ServerWidget::on_pushButton_2_clicked()
{
if(NULL == tcpSocket)
{
return;
}
//主動和客戶端端口連接
tcpSocket->disconnectFromHost();
tcpSocket->close();
tcpSocket = NULL;
}
客戶端:
文章來源地址http://www.zghlxwxcb.cn/news/detail-497047.html
clientwidget.h
#ifndef CLIENTWIDGET_H
#define CLIENTWIDGET_H
#include <QWidget>
#include <QTcpSocket>//通信套接字
namespace Ui {
class ClientWidget;
}
class ClientWidget : public QWidget
{
Q_OBJECT
public:
explicit ClientWidget(QWidget *parent = 0);
~ClientWidget();
private slots:
void on_buttonConnect_clicked();
void on_buttonSend_clicked();
void on_buttonClose_clicked();
private:
Ui::ClientWidget *ui;
QTcpSocket *tcpSocket;//通信套接字
};
#endif // CLIENTWIDGET_H
clientwidget.cpp
#include "clientwidget.h"
#include "ui_clientwidget.h"
#include <QHostAddress>
ClientWidget::ClientWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ClientWidget)
{
ui->setupUi(this);
tcpSocket = NULL;
tcpSocket = new QTcpSocket(this);
setWindowTitle("客戶端");
connect(tcpSocket,&QTcpSocket::connected,[=](){
ui->textEditRead->setText("成功和服務(wù)器建立好連接");
});
connect(tcpSocket,&QTcpSocket::readyRead,[=](){
//獲取對方發(fā)送的內(nèi)容
QByteArray array = tcpSocket->readAll();
//追加到編輯區(qū)中
array = "服務(wù)端:" + array;
ui->textEditRead->append(array);
});
}
ClientWidget::~ClientWidget()
{
delete ui;
}
void ClientWidget::on_buttonConnect_clicked()
{
//獲取服務(wù)器IP和端口
QString ip = ui->lineEditIP->text();
qint16 port = ui->lineEditPort->text().toInt();
//主動與服務(wù)器建立連接
tcpSocket->connectToHost(QHostAddress(ip),port);
}
void ClientWidget::on_buttonSend_clicked()
{
//獲取編輯框內(nèi)容
QString str = ui->textEditWrite->toPlainText();
//發(fā)送數(shù)據(jù)
tcpSocket->write(str.toUtf8().data());
ui->textEditWrite->setText("");
}
void ClientWidget::on_buttonClose_clicked()
{
//主動和對方斷開連接
tcpSocket->disconnectFromHost();
tcpSocket->close();
}
到了這里,關(guān)于Qt下Tcp套接字(socket)通信的整個流程的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!