大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
在QTcpSocket中的tcp通信,发送数据,接收数据都可以是QTcpSocket套接字的完成,包括server端,QTcpServer的功能仅仅是作为一个服务器的存在,它只是用来判断是不是有设备连接,连到以后的数据收发,还是用的QTcpSocket;
客户端:
class Client : public QWidget
{
Q_OBJECT
public:
explicit Client(QWidget *parent = 0);
~Client();
public slots:
void onReadyRead();//有数据接收触发
void onConnected();//连接成功触发
void onDisconnected();//断开连接触发
void on_pushButton_clicked();//连接IP
void on_pushButton_2_clicked();//发送数据
void on_pushButton_3_clicked();//断开连接
private:
Ui::Client *ui;
QTcpSocket*m_TcpSocket;
};
Client::Client(QWidget *parent) :
QWidget(parent),
ui(new Ui::Client){
ui->setupUi(this);
m_TcpSocket=new QTcpSocket;
//当socket上有新数据可读时,自动触发
connect(m_TcpSocket,SIGNAL(readyRead()),this,SLOT(onReadyRead()));
connect(m_TcpSocket,SIGNAL(connected()),this,SLOT(onConnected()));//连接成功触发
connect(m_TcpSocket,SIGNAL(disconnected()),this,SLOT(onDisconnected()));//断开连接触发
}
void Client::onReadyRead(){
QByteArray ba=m_TcpSocket->readAll();//读取所有数据
ui->lineEdit_3->setText(ba.data());
}
void Client::onConnected(){
qDebug()<<“连接成功”;
}
void Client::onDisconnected(){
qDebug()<<“断开连接”;
}
void Client::on_pushButton_clicked(){
QString IPstr=ui->lineEdit->text();
m_TcpSocket->connectToHost(IPstr,5555);//连接IP
qDebug()<<“尝试连接IP”;
m_TcpSocket->waitForConnected();//等待固定时长来连接
}
void Client::on_pushButton_2_clicked(){
QString str=ui->lineEdit_2->text();
m_TcpSocket->write(str.toLatin1());
qDebug()<<“发送数据”;
}
void Client::on_pushButton_3_clicked(){
m_TcpSocket->disconnectFromHost();//断开连接
}
服务器:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void onNewConnection();//收到请求连接信号触发
void onReadMessage();//读取信息触发
private slots:
void on_pushButton_clicked();//发送数据
void on_pushButton_2_clicked();//断开连接
private:
Ui::MainWindow *ui;
QTcpServer*m_TcpServer;
QTcpSocket*m_TcpClient;
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_TcpServer=new QTcpServer;
m_TcpServer->listen(QHostAddress::Any,5555);
//新连接信号触发
connect(m_TcpServer,SIGNAL(newConnection()),this,SLOT(onNewConnection()));
m_TcpClient=m_TcpServer->nextPendingConnection();//获取连接进来的socket
connect(m_TcpClient,SIGNAL(readyRead()),this,SLOT(onReadMessage()));
connect(m_TcpClient,SIGNAL(connected()),this,SLOT(onReadMessage()));
move(100,100);//设置窗口初始显示相对屏幕的位置
Client*client=new Client;
client->move(1000,100); //设置窗口初始显示相对屏幕的位置
client->show();
}
void MainWindow::onNewConnection(){
qDebug()<<“收到请求连接信号”;
m_TcpClient=m_TcpServer->nextPendingConnection();//得到连接进来的Socket
//有可读的信息,触发读取信息的槽函数
connect(m_TcpClient,SIGNAL(readyRead()),this,SLOT(onReadMessage()));
QString ipStr=m_TcpClient->peerAddress().toString();//获取对方的IP
}
void MainWindow::onReadMessage(){
qDebug()<<“读取信息”;
QByteArray ba=m_TcpClient->readAll();//接收数据
ui->lineEdit->setText(ba.data());
}
void MainWindow::on_pushButton_clicked(){
QString str=ui->lineEdit->text();
m_TcpClient->write(str.toLatin1());//发送数据
}
void MainWindow::on_pushButton_2_clicked(){
m_TcpClient->disconnectFromHost();//断开连接
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/195409.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...