zl程序教程

您现在的位置是:首页 >  大数据

当前栏目

qtcpsocket用法_qtcpsocket发送16进制数据

数据 用法 发送 16 进制 QTcpSocket
2023-06-13 09:13:38 时间

大家好,又见面了,我是你们的朋友全栈君。

在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();//断开连接

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/195409.html原文链接:https://javaforall.cn