博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
qt tcp socket通信实现字符串传输
阅读量:7248 次
发布时间:2019-06-29

本文共 2373 字,大约阅读时间需要 7 分钟。

QTcpServer的基本操作: 1、调用listen监听端口。 2、连接信号newConnection,在槽函数里调用nextPendingConnection获取连接进来的socket。

QTcpSocket的基本能操作: 1、调用connectToHost连接服务器。 2、调用waitForConnected判断是否连接成功。 3、连接信号readyRead槽函数,异步读取数据。 4、调用waitForReadyRead,阻塞读取数据。

服务器端

  1. 新建一个服务器端工程,填入
QT += network复制代码
#include
复制代码
  1. mainwindows.h文件中加入成员
public:    void init();private slots:    void sendMessage(); //发送消息    void onReciveData();  //接收数据    void newListen(); //建立tcp监听事件    void acceptConnection(); //接收客户端连接    void showError(QAbstractSocket::SocketError); //错误输出private:    QTcpSocket *tcpSocket;    QTcpServer *tcpServer;   // QTimer *timer;复制代码
  1. 函数的定义
MainWindow::MainWindow(QWidget *parent) :    QMainWindow(parent),    ui(new Ui::MainWindow){    ui->setupUi(this);    init();    setWindowTitle(QString::fromLocal8Bit("Server"));    connect(ui->sendBtn,SIGNAL(clicked(bool)),SLOT(sendMessage()));}复制代码

构造函数中,调用init()函数,当点击了发送按钮就会产生一个槽函数sendMessage,然后直接调用这个槽函数。

void MainWindow::init(){   // timer = new QTimer;    tcpServer = new QTcpServer;    tcpSocket = new QTcpSocket;    newListen();    connect(tcpServer,SIGNAL(newConnection()),SLOT(acceptConnection()));    connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),SLOT(showError(QAbstractSocket::SocketError)));}复制代码

新建一个QTcpServer类的对象,和QTcpSocket类的对象。调用newlisten()监听服务器的socket请求函数,有新的连接的时候,调用acceptConnection()函数,失败的话调用showError()。

void MainWindow::newListen(){    if(!tcpServer->listen(QHostAddress::Any,6666))    {        qDebug()<
errorString(); tcpServer->close(); }}复制代码

QHostAddress::Any 4 双any-address栈,与该地址绑定的socket将侦听IPv4,6666端口。

void MainWindow::acceptConnection(){    tcpSocket = tcpServer->nextPendingConnection();    connect(tcpSocket,SIGNAL(readyRead()),SLOT(onReciveData()));}复制代码

连接成功后,连接信号readyRead(),调用onReciveData()。

void MainWindow::sendMessage()  //发送数据{    QString textEdit = ui->lineEdit->text();    QString strData =QString::fromLocal8Bit("Time: ") + QTime::currentTime().toString() + "\n" + textEdit.toLocal8Bit() + "\n";    QByteArray sendMessage = strData.toLocal8Bit();    mChat += ("Send " + sendMessage);    ui->textEdit->setText(mChat);    tcpSocket->write(sendMessage);}void MainWindow::onReciveData()  //读取数据{    QString data = tcpSocket->readAll();    qDebug()<
textEdit->setText(mChat);}复制代码

发送数据,获取编辑框中的字符串,发送。

转载于:https://juejin.im/post/5a33b4076fb9a045186acbf8

你可能感兴趣的文章
〖C++〗string2int把字符串转换成int的函数
查看>>
Android使用SAX解析XML(1)
查看>>
taglib例子
查看>>
Android Sqite数据库 <9>
查看>>
苹果开发——App内购以及验证store的收据(一)
查看>>
[Python]网络爬虫(四):Opener与Handler的介绍和实例应用(转)
查看>>
学习博客
查看>>
Hive 实战(1)--hive数据导入/导出基础
查看>>
选择排序、插入排序、冒泡排序python实现
查看>>
关于GC的意见
查看>>
azure存储压测的问题(农码主观意识太强被坑了)
查看>>
手机新功能
查看>>
关于Opengl中将24位BMP图片加入一个alpha通道并实现透明的问题
查看>>
ScheduledExecutorService定时周期运行指定的任务
查看>>
Android 后台线程,timertask实现定期更新时间
查看>>
【Android】监听Notification被清除
查看>>
.NET设计模式(5):工厂方法模式(Factory Method)(转)
查看>>
Hadoop 新 MapReduce 框架 Yarn 详解
查看>>
系统启动方式
查看>>
读研究生期间你应该知道的50件事
查看>>