您的位置:首页 > 汽车 > 新车 > 买东西最便宜的网站_2023年河北石家庄最新疫情_百度云官方网站_网站seo策划方案

买东西最便宜的网站_2023年河北石家庄最新疫情_百度云官方网站_网站seo策划方案

2024/10/22 23:03:25 来源:https://blog.csdn.net/hdfinder/article/details/142881811  浏览:    关键词:买东西最便宜的网站_2023年河北石家庄最新疫情_百度云官方网站_网站seo策划方案
买东西最便宜的网站_2023年河北石家庄最新疫情_百度云官方网站_网站seo策划方案

服务器

文件结构
首先要在.pro文件中添加network,否则将不能使用QTcpserver

QT       += core gui network
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#define PORT 8000QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void newClientHandler();void clientInfoSlot();void on_sendButton_clicked();private:Ui::Widget *ui;QTcpServer *server;QTcpSocket *socket;
};
#endif // WIDGET_H
#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::newClientHandler);
}Widget::~Widget()
{delete ui;
}
void Widget::newClientHandler()
{//建立TCP连接socket = server->nextPendingConnection();ui->addresslineEdit->setText(socket->peerAddress().toString());ui->portlineEdit->setText(QString::number(socket->peerPort()));connect(socket,&QTcpSocket::readyRead,this,&Widget::clientInfoSlot);}
void Widget::clientInfoSlot()
{//获取信号的发出者QTcpSocket *socket=(QTcpSocket *)sender();ui->revlineEdit->setText(QString(socket->readAll()));
}void Widget::on_sendButton_clicked()
{QByteArray ba;ba.append(ui->sendlineEdit->text().toLatin1());socket->write(ba);
}

在这里插入图片描述

客户端

在这里插入图片描述
同样的,别忘了在.pro文件中修改

QT       += core gui network
#ifndef CHAT_H
#define CHAT_H#include <QWidget>
#include<QTcpSocket>
namespace Ui {
class chat;
}class chat : public QWidget
{Q_OBJECTpublic:explicit chat(QTcpSocket *socket,QWidget *parent = nullptr);~chat();private slots:void on_sendButton_clicked();void on_clearButton_clicked();void clientReadHandler();private:Ui::chat *ui;QTcpSocket *socket;
};#endif // CHAT_H
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include<QTcpsocket>
#include<QHostAddress>
#include<QMessageBox>
#include "chat.h"
QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_cancelButton_clicked();void on_connectButton_clicked();private:Ui::Widget *ui;QTcpSocket *socket;chat *ch;
};
#endif // WIDGET_H
#include "chat.h"
#include "ui_chat.h"chat::chat(QTcpSocket *s,QWidget *parent) :QWidget(parent),ui(new Ui::chat)
{ui->setupUi(this);socket = s;connect(socket,&QTcpSocket::readyRead,this,&chat::clientReadHandler);
}chat::~chat()
{delete ui;
}void chat::on_sendButton_clicked()
{QByteArray ba;ba.append(ui->lineEdit->text().toLatin1());socket->write(ba);
}void chat::on_clearButton_clicked()
{ui->lineEdit->clear();
}void chat::clientReadHandler()
{ui->revlineEdit->setText(QString(socket->readAll()));
}
#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_connectButton_clicked()
{QString ip = ui->iplineEdit->text();QString port = ui->portlineEdit->text();qDebug() << ip;qDebug() << port;socket->connectToHost(QHostAddress(ip),port.toShort());connect(socket,&QTcpSocket::connected,[this]{QMessageBox::information(this,"Tip","连接成功");this->hide();ch = new chat(socket);ch->show();});connect(socket,&QTcpSocket::disconnected,[this]{QMessageBox::information(this,"Tip","连接失败!");});
}void Widget::on_cancelButton_clicked()
{this->close();
}

在这里插入图片描述
在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com