60
ClientWindow::ClientWindow(QWidget* parent):QWidget(parent) {
//创建QTCPSocket指针对象
socket = new QTcpSocket(this);
//设置大小
setFixedSize(600,600);
//设置标题
setWindowTitle("客户端");
//创建布局
QVBoxLayout *layout = new QVBoxLayout;
QHBoxLayout *hl = new QHBoxLayout;
QFormLayout*fl = new QFormLayout;
QLineEdit*ipEdit = new QLineEdit;
QLineEdit*portEdit = new QLineEdit;
ipEdit->setText("127.0.0.1");
portEdit->setText("6666");
QPushButton*connectBtn = new QPushButton("连接");
fl->addRow("服务器 ip:",ipEdit);
fl->addRow("服务器端口:",portEdit);
hl->addLayout(fl);
hl->addWidget(connectBtn);
QTextEdit *recvMsg = new QTextEdit;
QTextEdit *sendMsg = new QTextEdit;
recvMsg->setEnabled(false);
QHBoxLayout *btnLayout = new QHBoxLayout;
QPushButton*sendBtn = new QPushButton("发送");
QPushButton*closeBtn = new QPushButton("关闭");
layout->addLayout(hl);
layout->addWidget(recvMsg);
layout->addWidget(sendMsg);
btnLayout->addWidget(sendBtn);
btnLayout->addWidget(closeBtn);
layout->addLayout(btnLayout);
setLayout(layout);
//信号和槽函数
connect(connectBtn,&QPushButton::clicked,[=](){
//获取ip和顿口
QString ip = ipEdit->text();
quint16 port = portEdit->text().toInt();
socket->connectToHost(ip,port);
connect(sendBtn,&QPushButton::clicked,[=](){
QString msg = sendMsg->toPlainText();
socket->write(msg.toUtf8().data());
//接收到新消息
connect(socket,&QTcpSocket::readyRead,this,[=](){
QByteArray data = socket->readAll();
recvMsg->append(QString(data));
2、服务端ServerWindow.cpp
65
ServerWindow::ServerWindow(QWidget *parent) : QWidget(parent) {
//设置大小
setFixedSize(600, 600);
//设置标题
setWindowTitle("服务器:6666");
//创建布局
QVBoxLayout *layout = new QVBoxLayout;
recvMsg = new QTextEdit;
sendMsg = new QTextEdit;
recvMsg->setEnabled(false);
QHBoxLayout *btnLayout = new QHBoxLayout;
QPushButton *sendBtn = new QPushButton("发送");
QPushButton *closeBtn = new QPushButton("关闭");
layout->addWidget(recvMsg);
layout->addWidget(sendMsg);
btnLayout->addWidget(sendBtn);
btnLayout->addWidget(closeBtn);
layout->addLayout(btnLayout);
setLayout(layout);
//初始化socket
initSocket();
connect(sendBtn,&QPushButton::clicked,[=](){
QString msg = sendMsg->toPlainText();
socket->write(msg.toUtf8().data());
ServerWindow::~ServerWindow() {
//初始化socket
void ServerWindow::initSocket() {
//创建socket
server = new QTcpServer(this);
server->listen(QHostAddress::Any, 6666);
//连接上的信号和槽函数
connect(server, &QTcpServer::newConnection, this, &ServerWindow::clientConnectToServer);
//连接上的信号和槽函数
void ServerWindow::clientConnectToServer() {
//保存通信套接字
socket = server->nextPendingConnection();
//获取连接的ip和端口
QString ip = socket->peerAddress().toString();
int port = socket->peerPort();
recvMsg->append(QString("[%1:%2]连接成功").arg(ip).arg(port));
//监听新消息
connect(socket,&QTcpSocket::readyRead,this,&ServerWindow::receiveMsg);
//有新消息
void ServerWindow::receiveMsg(){
QByteArray data = socket->readAll();
recvMsg->append(QString(data));
2. udp通信
udp通信不需要连接服务端操作
1、客户端ClientWindow.cpp
51
ClientWindow::ClientWindow(QWidget *parent) : QWidget(parent) {
socket = new QUdpSocket(this);
//设置大小
setFixedSize(600, 600);
//设置标题
setWindowTitle("客户端:8888");
//创建布局
QVBoxLayout *layout = new QVBoxLayout;
QHBoxLayout *hl = new QHBoxLayout;
QFormLayout *fl = new QFormLayout;
QLineEdit *ipEdit = new QLineEdit;
QLineEdit *portEdit = new QLineEdit;
ipEdit->setText("127.0.0.1");
portEdit->setText("6666");
QPushButton *connectBtn = new QPushButton("连接");
fl->addRow("服务器 ip:", ipEdit);
fl->addRow("服务器端口:", portEdit);
hl->addLayout(fl);
hl->addWidget(connectBtn);
QTextEdit *recvMsg = new QTextEdit;
QTextEdit *sendMsg = new QTextEdit;
recvMsg->setEnabled(false);
QHBoxLayout *btnLayout = new QHBoxLayout;
QPushButton *sendBtn = new QPushButton("发送");
QPushButton *closeBtn = new QPushButton("关闭");
layout->addLayout(hl);
layout->addWidget(recvMsg);
layout->addWidget(sendMsg);
btnLayout->addWidget(sendBtn);
btnLayout->addWidget(closeBtn);
layout->addLayout(btnLayout);
setLayout(layout);
socket->bind(8888, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
connect(sendBtn, &QPushButton::clicked, [=]() {
QString ip = ipEdit->text();
int port = portEdit->text().toInt();
QString msg = sendMsg->toPlainText();
socket->writeDatagram(msg.toUtf8(),QHostAddress(ip),port);
2、服务端ServerWindow.cpp
52
ServerWindow::ServerWindow(QWidget *parent) : QWidget(parent) {
socket = new QUdpSocket(this);
//设置大小
setFixedSize(600, 600);
//设置标题
setWindowTitle("客户端:6666");
//创建布局
QVBoxLayout *layout = new QVBoxLayout;
QHBoxLayout *hl = new QHBoxLayout;
QFormLayout *fl = new QFormLayout;
QLineEdit *ipEdit = new QLineEdit;
QLineEdit *portEdit = new QLineEdit;
ipEdit->setText("127.0.0.1");
portEdit->setText("8888");
QPushButton *connectBtn = new QPushButton("连接");
fl->addRow("服务器 ip:", ipEdit);
fl->addRow("服务器端口:", portEdit);
hl->addLayout(fl);
hl->addWidget(connectBtn);
QTextEdit *recvMsg = new QTextEdit;
QTextEdit *sendMsg = new QTextEdit;
recvMsg->setEnabled(false);
QHBoxLayout *btnLayout = new QHBoxLayout;
QPushButton *sendBtn = new QPushButton("发送");
QPushButton *closeBtn = new QPushButton("关闭");
layout->addLayout(hl);
layout->addWidget(recvMsg);
layout->addWidget(sendMsg);
btnLayout->addWidget(sendBtn);
btnLayout->addWidget(closeBtn);
layout->addLayout(btnLayout);
setLayout(layout);
socket->bind(6666, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
connect(socket, &QUdpSocket::readyRead, [=]() {
// QString msg = socket->readAll();
QByteArray data;
data.resize(socket->pendingDatagramSize());
socket->readDatagram(data.data(), data.size());
qDebug() << "接收到消息" << data << endl;
socket->close();
powered by
MkDocs
Material for MkDocs