QT下基于TCP协议的网络通信分为服务端和客服端程序两部分,与之前写的C网络通信不同的是QT下服务器端只需要定义一个服务器对象和用来进行通信的TcpSocket,只需通过监听和连接实现与客服端的连接;客服端中需要定义一个Tcpsocket,通过connectToHost(IP,端口号)进行连接。部分源代码如下:
服务器端:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include
#include// tcp 服务器类,=====> 负责监听是否有客户端连接
#include// tcp socket ======> 负责tcp的通信
namespace Ui {
class widget;
}
class widget : public QWidget
{
Q_OBJECT
public:
explicit widget(QWidget *parent = 0);
~widget();
public slots:
void newClient();
void readData();
private:
Ui::widget *ui;
QTcpServer server; // 定义一个服务器对象
};
#endif // WIDGET_H
chat.h
#ifndef CHAT_H
#define CHAT_H
#include
#include
#include
namespace Ui {
class Chat;
}
class Chat : public QWidget
{
Q_OBJECT
public:
explicit Chat(QTcpServer *tcpServer,QWidget *parent = 0);
~Chat();
private slots:
void on_pushButton_clicked();
private:
Ui::Chat *ui;
QTcpSocket *s;
};
#endif // CHAT_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include
#include
widget::widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::widget)
{
ui->setupUi(this);
// 监听本地的 ip 和端口
server.listen(QHostAddress::Any, 9999);
connect(&server, SIGNAL(newConnection()), this, SLOT(newClient()));
}
widget::~widget()
{
delete ui;
}
void widget::newClient()
{
while(server.hasPendingConnections()) //一次接受所有客服端的请求
{
Chat *c = new Chat(&server);
c->show();
c->setAttribute(Qt::WA_DeleteOnClose);
}
}
void widget::readData()
{
}
chat.cpp
#include "widget.h"
#include "ui_widget.h"
#include
#include
widget::widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::widget)
{
ui->setupUi(this);
// 监听本地的 ip 和端口
server.listen(QHostAddress::Any, 9999);
connect(&server, SIGNAL(newConnection()), this, SLOT(newClient()));
}
widget::~widget()
{
delete ui;
}
void widget::newClient()
{
while(server.hasPendingConnections()) //一次接受所有客服端的请求
{
Chat *c = new Chat(&server);
c->show();
c->setAttribute(Qt::WA_DeleteOnClose);
}
}
void widget::readData()
{
}
客服端:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
namespace Ui {
class widget;
}
class widget : public QWidget
{
Q_OBJECT
public:
explicit widget(QWidget *parent = 0);
~widget();
private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked();
private:
Ui::widget *ui;
QTcpSocket socket; //用来和服务器进行通信
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include
#include
widget::widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::widget)
{
ui->setupUi(this);
connect(&socket,&QTcpSocket::connected,
[this](){
ui->pushButton->setEnabled(true);
ui->pushButton_2->setEnabled(false);
QMessageBox::information(this,"连接服务器","已经连接服务器");
});
connect(&socket,&QTcpSocket::readyRead,
[this]{
while(socket.bytesAvailable())
{
QByteArray data = socket.readAll();
QString str = QString("服务返回消息:%1").arg(QString(data));
ui->textEdit->append(str);
}
});
}
widget::~widget()
{
delete ui;
}
void widget::on_pushButton_2_clicked()
{
QString ip = ui->lineEdit->text();
qint16 port=ui->lineEdit_2->text().toInt();
socket.connectToHost(QHostAddress(ip),port);
}
void widget::on_pushButton_clicked()
{
QString data = ui->textEdit->toPlainText();
socket.write(data.toUtf8());
}