我们需要实现客户端发消息,服务端接收消息
服务端界面新增接收消息
实现客户端发送和清空
发送数据需要将发送栏的信息转化为QByteArray,然后使用socket的write发送过去
实现服务端的接收
效果演示
20240818_111603
代码展示
server
Widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QTcpSocket>
#include<QTcpServer>
#define PORT 8080
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void deal();
void serverrecv();
private:
Ui::Widget *ui;
QTcpServer *server;
};
#endif // WIDGET_H
Widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
server=new QTcpServer;
server->listen(QHostAddress::AnyIPv4,PORT);
connect(server,&QTcpServer::newConnection,this,&Widget::deal);
}
Widget::~Widget()
{
delete ui;
}
void Widget::deal()
{
QTcpSocket* socket=server->nextPendingConnection();
ui->clientip->setText(socket->peerAddress().toString());
ui->clientport->setText(QString::number(socket->peerPort()));
connect(socket,&QTcpSocket::readyRead,this,&Widget::serverrecv);
}
void Widget::serverrecv()
{
QTcpSocket* socket=(QTcpSocket*)sender();
// socket->readAll();//QBytearray
ui->recvinfo->setText(QString(socket->readAll()));
}
client
chat.h
#ifndef CHAT_H
#define CHAT_H
#include <QWidget>
#include<QTcpSocket>
namespace Ui {
class chat;
}
class chat : public QWidget
{
Q_OBJECT
public:
explicit chat(QTcpSocket*s,QWidget *parent = nullptr);
~chat();
private slots:
void on_clearbutton_clicked();
void on_sendbutton_clicked();
private:
Ui::chat *ui;
QTcpSocket*socket;
};
#endif // CHAT_H
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QTcpSocket>
#include<QString>
#include <QHostAddress>
#include<QMessageBox>
#include<chat.h>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_closebutton_clicked();
void on_connectbutton_clicked();
private:
Ui::Widget *ui;
QTcpSocket* socket;
};
#endif // WIDGET_H
chat.cpp
#include "chat.h"
#include "ui_chat.h"
chat::chat(QTcpSocket *s, QWidget *parent) :
QWidget(parent),
ui(new Ui::chat)
{
ui->setupUi(this);
socket=s;
}
chat::~chat()
{
delete ui;
}
void chat::on_clearbutton_clicked()
{
ui->sendline->clear();
}
void chat::on_sendbutton_clicked()
{
QByteArray sa;
sa.append(ui->sendline->text());
socket->write(sa);
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
socket=new QTcpSocket;
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_closebutton_clicked()
{
this->close();
}
void Widget::on_connectbutton_clicked()
{
QString ip=ui->iplabel->text();
QString port=ui->portlabel->text();
socket->connectToHost(QHostAddress(ip),port.toShort());
connect(socket,&QTcpSocket::connected,[this]()
{
QMessageBox::information(this,"连接提示","连接成功");
this->hide();
chat *newui=new chat(socket);
newui->show();
});
connect(socket,&QTcpSocket::disconnected,[this]()
{
QMessageBox::warning(this,"连接提示","连接异常");
});
}