1. 客户端和服务器端ui设计
- 客户端
-
服务器端
2. 客户端和服务器端头文件编写 -
客户端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_pushButton_connect_clicked();
void on_pushButto_send_clicked();
void on_pushButton_close_clicked();
private:
Ui::clientwidget *ui;
QTcpSocket *tcpSocket;
};
#endif // CLIENTWIDGET_H
- 服务器端serwidget.h
#ifndef SERWIDGET_H
#define SERWIDGET_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
namespace Ui {
class SerWidget;
}
class SerWidget : public QWidget
{
Q_OBJECT
public:
explicit SerWidget(QWidget *parent = 0);
~SerWidget();
private slots:
void on_button_send_clicked();
void on_button_close_clicked();
private:
Ui::SerWidget *ui;
QTcpServer *tcpServer;//监听套接字
QTcpSocket *tcpSocket;//通信套接字
};
#endif // SERWIDGET_H
3. 客户端和服务器端cpp实现和main函数
- 客户端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->textEdit_read->setText("连接成功!");
}
);
connect(tcpSocket,&QTcpSocket::readyRead,
[=]()
{
//获取对方发来的信息
QByteArray drray = tcpSocket->readAll();
ui->textEdit_read->append(drray);
}
);
}
clientwidget::~clientwidget()
{
delete ui;
}
void clientwidget::on_pushButton_connect_clicked()
{
QString ip = ui->lineEdit_ip->text();
qint16 port = ui->lineEdit_port->text().toInt();
tcpSocket->connectToHost(QHostAddress(ip),port);
}
void clientwidget::on_pushButto_send_clicked()
{
//获取编辑内容
QString str = ui->textEdit_send->toPlainText();
//发送数据
tcpSocket->write(str.toUtf8().data());
ui->textEdit_send->clear();
}
void clientwidget::on_pushButton_close_clicked()
{
//主动和对方断开连接
tcpSocket->disconnectFromHost();
tcpSocket->close();
}
- 服务器端serwidget.cpp
#include "serwidget.h"
#include "ui_serwidget.h"
SerWidget::SerWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::SerWidget)
{
tcpServer = NULL;
tcpServer = NULL;
ui->setupUi(this);
//监听套接字,指定父对象,自动回收空间
tcpServer = new QTcpServer(this);
tcpServer->listen(QHostAddress::Any,8000);
setWindowTitle("服务器:8000");
connect(tcpServer,&QTcpServer::newConnection,
[=]()
{
//取出建立好的连接
tcpSocket = tcpServer->nextPendingConnection();
//获取对方的ip和端口
QString ip = tcpSocket->peerAddress().toString();
qint16 port = tcpSocket->peerPort();
qDebug()<<ip<<","<<port;
QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port);
ui->textEdit_read->setText(temp);
connect(tcpSocket,&QTcpSocket::readyRead,
[=]()
{
QByteArray drray = tcpSocket->readAll();
ui->textEdit_read->append(drray);
});
}
);
}
SerWidget::~SerWidget()
{
delete ui;
}
void SerWidget::on_button_send_clicked()
{
if (NULL == tcpSocket)
return ;
//获取缓冲区的内容
QString str = ui->textEdit_send->toPlainText();
//给对方发送数据,使用套接字tcpsocket
tcpSocket->write(str.toUtf8().data());
ui->textEdit_send->clear();
}
void SerWidget::on_button_close_clicked()
{
if (NULL == tcpSocket)
return ;
tcpSocket->disconnectFromHost();
tcpSocket->close();
tcpSocket = NULL;
}
- main.cpp
#include "serwidget.h"
#include <QApplication>
#include "clientwidget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SerWidget w;
clientwidget w2;
w2.show();
w.show();
return a.exec();
}
4. 效果展示
- 客户端
- 服务器端