Qt 中关于socket的读与写
来源:互联网 发布:mac pro 忘记开机密码 编辑:程序博客网 时间:2024/05/16 05:40
大部分是转载
服务器端:
// server #include "testnet.h" #include "ui_testnet.h" #include <QtGui> Testnet::Testnet(QWidget *parent) : QMainWindow(parent), ui(new Ui::Testnet) { ui->setupUi(this); this->connect(ui->pushButton_start,SIGNAL(clicked()),this,SLOT(startTcpserver())); this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMessage())); } Testnet::~Testnet() { delete ui; } void Testnet::startTcpserver() { m_tcpServer = new QTcpServer(this); m_tcpServer->listen(QHostAddress::Any,19999); //监听任何连上19999端口的ip connect(m_tcpServer,SIGNAL(newConnection()),this,SLOT(newConnect())); //新连接信号触发,调用newConnect()槽函数,这个跟信号函数一样,其实你可以随便取。 } void Testnet::newConnect() { m_tcpSocket = m_tcpServer->nextPendingConnection(); //得到每个连进来的socket connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMessage())); //有可读的信息,触发读函数槽 } void Testnet::readMessage() //读取信息 { QByteArray qba= m_tcpSocket->readAll(); //读取 qDebug()<<qba; QString ss=QVariant(qba).toString(); ui->textEdit_rec->setText(ss); } void Testnet::sendMessage() //发送信息 { QString strMesg= ui->lineEdit_sendmessage->text(); qDebug()<<strMesg; m_tcpSocket->write(strMesg.toStdString().c_str(),strlen(strMesg.toStdString().c_str())); //发送 } 客户端:// client #include "testnet_c.h" #include "ui_testnet_c.h" testnet_c::testnet_c(QWidget *parent) : QMainWindow(parent), ui(new Ui::testnet_c) { ui->setupUi(this); this->connect(ui->pushButton_con,SIGNAL(clicked()),this,SLOT(connectServer())); this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMesg())); } testnet_c::~testnet_c() { delete ui; } void testnet_c::connectServer() { m_tcpSocket = new QTcpSocket(this); m_tcpSocket->abort(); m_tcpSocket->connectToHost("192.168.1.178",19999); connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMesg())); } void testnet_c::readMesg() //读取信息 { QByteArray qba = m_tcpSocket->readAll(); ui->textEdit_recmesg->clear(); qDebug()<<qba; QString ss=QVariant(qba).toString(); ui->textEdit_recmesg->setText(ss); } void testnet_c::sendMesg() //发送信息 { QString ss= ui->lineEdit_sendmesg->text(); m_tcpSocket->write(ss.toStdString().c_str(),strlen(ss.toStdString().c_str())); ui->lineEdit_sendmesg->clear(); }可见,在通信过程中,读信息需要检测信号,而写信息却可以直接进行
- Qt 中关于socket的读与写
- 如何在qt中使用底层的connect函数来写socket
- 怎么在qt中使用connect指令来写socket
- QT中关于信号与槽机制的实现原理
- QT中关于信号与槽机制的实现原理
- QT中关于信号与槽机制的实现原理
- QT中关于信号与槽机制的实现原理
- QT中关于信号与槽机制的实现原理
- QT中关于信号与槽机制的实现原理
- QT socket 与 linux socket 的字符转换
- QT 中加入socket
- Qt中socket编程
- QT中Socket编程
- Qt中socket编程
- Qt中socket编程
- Qt中socket编程
- asp中类的写与读!
- 关于QT写好的软件的打包问题。
- 推荐一系列优秀的Android开发源码
- 很多人看都哭了,不信你不哭
- SQL Server 中,实现 varbinary 与 varchar 类型之间的数据转换
- hadoop集群运行异常故障描述
- 吃一堑,长一智。
- Qt 中关于socket的读与写
- 使用重定向 redirects
- crontab 调用shell 不执行解决办法
- java的接口与抽象类的使用
- 《Effective C#》读书笔记——了解.NET内存管理机制<.NET资源管理>
- 二八定律 80/20法则 帕累托法则、帕累托定律、马特莱定律、最省力法则或不平衡原则
- oracle创建用户、授权、表空间等
- log4j的配置解析
- 推荐一系列优秀的Android开发源码