QT学习记录(二)QT+redis小实例

  这个小实例是在Linux系统下用QT开发的,客户端和服务器端通过redis为媒介通信,这个程序主要实现客户端发送一个字符串,服务器端接收到该字符串,并且转换为大写。特别需要注意的是,Linux可能会出现找不到动态链接库的情况。此时我们需要在pro文件中加上链接地址。不然编译时会报错。

该程序中用到redis的库和线程pthread的库 加入
LIBS +=-L/usr/local/lib -lhiredis -lpthread

1.客户端

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <hiredis/hiredis.h>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    redisContext* pRedisContext=(redisContext*)redisConnect("127.0.0.1",6379);
    if(pRedisContext==NULL)
    {
        printf("Error:连接失败\n");
        return ;
    }
    if(pRedisContext->err!=0)
    {
        printf("Error:%s\n",pRedisContext->errstr);
        redisFree(pRedisContext);
    }
    QString s=ui->lineEdit->text();
    QByteArray ba=s.toLatin1();
    char *ss=ba.data();//string转换为char*类型
    redisReply* reply=(redisReply*)redisCommand(pRedisContext,"lpush RequestList %s",ss);
    if(reply->type!=REDIS_REPLY_INTEGER)
    {
        printf("Failed");
    }
    freeReplyObject(reply);
    redisReply* reply1=(redisReply*)redisCommand(pRedisContext,"blpop ReplyList 5");
    if(reply1->type==REDIS_REPLY_NIL)
    {
        QMessageBox::warning(this,tr("Warning"),tr("timeout!"),QMessageBox::Yes);
    }
    else
    {
        QString str=reply1->element[1]->str;
        ui->textEdit->setText(str);
    }
    freeReplyObject(reply1);
    redisFree(pRedisContext);//释放资源
}

2.服务器端

main.c

#include <stdio.h>
#include <hiredis/hiredis.h>
#include <string.h>
#include <malloc.h>
#include <time.h>
#include <sys/syscall.h>
#include <stdbool.h>
#include <pthread.h>
bool judge=true;
void threadredis()
{
    printf("开始子线程ID号:%u\n",pthread_self());
    int i;
    char *st=(char*)malloc(sizeof(char));
    struct tm *p;
    redisContext* pRedisContext=(redisContext*)redisConnect("127.0.0.1",6379);
    if(pRedisContext==NULL)
    {
        printf("Error:连接失败!\n");
        return ;
    }
    if(pRedisContext->err!=0)
    {
        printf("Error:%s\n",pRedisContext->errstr);
        redisFree(pRedisContext);
    }

    while(judge)
    {
        redisReply* reply=(redisReply*)redisCommand(pRedisContext,"blpop RequestList 5");
        if(reply->type==REDIS_REPLY_NIL)
        {
            freeReplyObject(reply);
        }
        else
        {
            time_t t=time(0);
            p=localtime(&t);
            printf("%d/%d/%d %d:%d:%d         ",p->tm_year+1900,p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
            st=reply->element[1]->str;
            printf("%s    ",st);
            for(i=0;st[i]!='\0';i++)
            {
                st[i]=toupper(st[i]);
            }
            puts(st);
            redisReply* reply1=(redisReply*)redisCommand(pRedisContext,"lpush ReplyList %s",st);
            freeReplyObject(reply);
            if(reply1->type!=REDIS_REPLY_INTEGER)
            {
                printf("Failed");
            }
            freeReplyObject(reply1);
        }
    }
    redisFree(pRedisContext);
    printf("thread end\n");
    printf("结束子线程ID号:%u\n",pthread_self());
}
int main(int argc, char *argv[])
{
    pthread_t son;
    char input;
    printf("S:开始 Q:结束子线程 E:退出\n");
    while(1)
    {
        input=getchar();
        if(input=='S')
        {
            pthread_create(&son,NULL,(void*)threadredis,NULL);
            judge=true;

        }
        if(input=='Q')
        {
            judge=false;
        }
        else if(input=='E')
        {
            pthread_join(son,NULL);
            exit(0);
        }
    }
    return 0;
}


 

  • 8
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值