一、QT如果要进行网络编程首先需要在.pro中添加如下代码:

QT += network

二、QT下的TCP通信过程

Qt中提供的所有的Socket类都是非阻赛的 Qt中常用的用于socket通信的套接字类         QTcpServer 用于TCP/IP通信,作为服务器端套接字使用         QTcpSocket 用于TCP/IP通信,作为客户端套接字使用。         QUdpSocket 用于UDP通信,服务器,客户端均使用此套接字

三、描述Qt下Tcp通信的整个流程 

服务器端:

1. 创建用于监听的套接字 

//监听套接字

tcpServer = new QTcpServer(this);

2. 给套接字设置监听 

tcpServer->listen(QHostAddress::Any,8888);

setWindowTitle("服务器:8888");

3. 如果有连接到来, 监听的套接字会发出信号newConnected  4. 接收连接, 通过nextPendingConnection()函数, 返回一个QTcpSocket类型的套接字对象(用于通信) 

connect(tcpServer,&QTcpServer::newConnection,[=](){

//取出建立好连接的套接字

tcpSocket = tcpServer->nextPendingConnection();

});

5. 使用用于通信的套接字对象通信  (1). 发送数据: write 

if(NULL == tcpSocket)

{

return;

}

//获取编辑区内容

QString str = ui->textEditWrite->toPlainText();

//给对方发送数据,使用套接字是tcpSocket

tcpSocket->write(str.toUtf8().data());

(2). 接收数据: readAll/read  

connect(tcpSocket,&QTcpSocket::readyRead,[=](){

//从通信套接字中取出内容

QByteArray array = tcpSocket->readAll();

array = "客户端;" + array;

ui->textEditRead->append(array);

});

客户端:

1. 创建用于通信的套接字 

tcpSocket = new QTcpSocket(this);

setWindowTitle("客户端");

2. 连接服务器: connectToHost 

//获取服务器IP和端口

QString ip = ui->lineEditIP->text();

qint16 port = ui->lineEditPort->text().toInt();

//主动与服务器建立连接

tcpSocket->connectToHost(QHostAddress(ip),port);

connect(tcpSocket,&QTcpSocket::connected,[=](){

ui->textEditRead->setText("成功和服务器建立好连接");

});

3. 连接成功与服务器通信  (1).发送数据: write 

//获取编辑框内容

QString str = ui->textEditWrite->toPlainText();

//发送数据

tcpSocket->write(str.toUtf8().data());

(2).接收数据: readAll/read

connect(tcpSocket,&QTcpSocket::readyRead,[=](){

//获取对方发送的内容

QByteArray array = tcpSocket->readAll();

//追加到编辑区中

array = "服务端:" + array;

ui->textEditRead->append(array);

});

四、完整代码

服务端:

 setverwidget.h

#ifndef SERVERWIDGET_H

#define SERVERWIDGET_H

#include

#include //监听套接字

#include //通信套接字

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;//监听套接字

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;

//监听套接字

tcpServer = new QTcpServer(this);

tcpServer->listen(QHostAddress::Any,8888);

setWindowTitle("服务器: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,[=](){

//从通信套接字中取出内容

QByteArray array = tcpSocket->readAll();

array = "客户端;" + array;

ui->textEditRead->append(array);

});

});

}

ServerWidget::~ServerWidget()

{

delete ui;

}

void ServerWidget::on_buttonSend_clicked()

{

if(NULL == tcpSocket)

{

return;

}

//获取编辑区内容

QString str = ui->textEditWrite->toPlainText();

//给对方发送数据,使用套接字是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;

}

客户端:

clientwidget.h

#ifndef CLIENTWIDGET_H

#define CLIENTWIDGET_H

#include

#include //通信套接字

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

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("成功和服务器建立好连接");

});

connect(tcpSocket,&QTcpSocket::readyRead,[=](){

//获取对方发送的内容

QByteArray array = tcpSocket->readAll();

//追加到编辑区中

array = "服务端:" + array;

ui->textEditRead->append(array);

});

}

ClientWidget::~ClientWidget()

{

delete ui;

}

void ClientWidget::on_buttonConnect_clicked()

{

//获取服务器IP和端口

QString ip = ui->lineEditIP->text();

qint16 port = ui->lineEditPort->text().toInt();

//主动与服务器建立连接

tcpSocket->connectToHost(QHostAddress(ip),port);

}

void ClientWidget::on_buttonSend_clicked()

{

//获取编辑框内容

QString str = ui->textEditWrite->toPlainText();

//发送数据

tcpSocket->write(str.toUtf8().data());

ui->textEditWrite->setText("");

}

void ClientWidget::on_buttonClose_clicked()

{

//主动和对方断开连接

tcpSocket->disconnectFromHost();

tcpSocket->close();

}

精彩文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: