Linux下cs构架聊天工具项目说明书_第1页
Linux下cs构架聊天工具项目说明书_第2页
Linux下cs构架聊天工具项目说明书_第3页
Linux下cs构架聊天工具项目说明书_第4页
Linux下cs构架聊天工具项目说明书_第5页
已阅读5页,还剩115页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

PAGEPAGE7TOC\o"1—4"\u项目介绍 PAGEREF_Toc165364930\h2什么是c/s构架ﻩPAGEREF_Toc165364931\h2什么是tcp/ipﻩPAGEREF_Toc165364932\h2什么是udp PAGEREF_Toc165364933\h3什么是MYSQLﻩPAGEREF_Toc165364934\h3MYSQL数据库配置 PAGEREF_Toc165364935\h3本次项目内容介绍ﻩPAGEREF_Toc165364936\h5需求分析ﻩPAGEREF_Toc165364937\h5程序效果的实现 5364938\h5登陆ﻩPAGEREF_Toc165364939\h5主界面ﻩPAGEREF_Toc165364940\h6主界面-功能表 PAGEREF_Toc165364941\h7主界面—群 PAGEREF_Toc165364942\h8用户查询 165364943\h9群查询ﻩPAGEREF_Toc165364944\h10单人谈天框ﻩPAGEREF_Toc165364945\h10群谈天框 PAGEREF_Toc165364946\h12查询结果ﻩ165364947\h13彩色字体 PAGEREF_Toc165364948\h14程序文件介绍 PAGEREF_Toc165364949\h14服务器ﻩPAGEREF_Toc165364950\h14数据库块 PAGEREF_Toc165364951\h14服务器块 PAGEREF_Toc165364952\h14客户端 PAGEREF_Toc165364953\h15登陆块 PAGEREF_Toc165364954\h15注册块ﻩPAGEREF_Toc165364955\h15xSelectIcon。h,xSelectIcon。cpp选择用户头像程序。 PAGEREF_Toc165364956\h15主界面块 PAGEREF_Toc165364957\h15私聊块 PAGEREF_Toc165364958\h15群聊块ﻩPAGEREF_Toc165364959\h15好友管理块ﻩPAGEREF_Toc165364960\h15程序代码详解ﻩPAGEREF_Toc165364961\h16服务器 PAGEREF_Toc165364962\h16数据库块 PAGEREF_Toc165364963\h16my_sql.hﻩPAGEREF_Toc165364964\h16close_sql.hﻩPAGEREF_Toc165364965\h23server.cppﻩPAGEREF_Toc165364966\h25server.h PAGEREF_Toc165364967\h28newClient。cppﻩPAGEREF_Toc165364968\h29newClient。hﻩPAGEREF_Toc165364969\h43serverForm。h PAGEREF_Toc165364970\h45客户端 PAGEREF_Toc165364971\h45登陆块 PAGEREF_Toc165364972\h45xLogin.hﻩPAGEREF_Toc165364973\h45xLogin.cpp PAGEREF_Toc165364974\h47注册块ﻩPAGEREF_Toc165364975\h51xRegister。hﻩPAGEREF_Toc165364976\h51xRegister。cppﻩPAGEREF_Toc165364977\h52xSelectIcon.hﻩPAGEREF_Toc165364978\h57xSelectIcon.cpp PAGEREF_Toc165364979\h57主界面块 PAGEREF_Toc165364980\h59xclient。hﻩPAGEREF_Toc165364981\h59client.cpp PAGEREF_Toc165364982\h62私聊块ﻩPAGEREF_Toc165364983\h83xchat.h PAGEREF_Toc165364984\h83chat。cpp PAGEREF_Toc165364985\h85xselectface.hﻩPAGEREF_Toc165364986\h92selectface.cppﻩPAGEREF_Toc165364987\h92xdownload。h PAGEREF_Toc165364988\h93download.cppﻩPAGEREF_Toc165364989\h94群聊块ﻩPAGEREF_Toc165364990\h95xteam.hﻩPAGEREF_Toc165364991\h95team.cppﻩPAGEREF_Toc165364992\h96xselectface1.hﻩPAGEREF_Toc165364993\h100selectface1.cpp PAGEREF_Toc165364994\h101好友管理块ﻩPAGEREF_Toc165364995\h102xsearch。hﻩPAGEREF_Toc165364996\h102xseach。cppﻩPAGEREF_Toc165364997\h103xaddF。hﻩ\h111xaddF.cppﻩPAGEREF_Toc165364999\h112项目介绍什么是c/s构架C/S(Client/Server,客户机/服务器)模式又称C/S结构,是软件系统体系结构的一种。C/S模式简洁地讲就是基于企业内部网络的应用系统。C/S模式的应用系统最大的好处是不依靠企业外网环境,即无论企业是否能够上网,都不影响应用.什么是tcp/ipﻩTCP/IP是TransmissionControlProtocol/InternetProtocol的简写,中文译名为传输掌握协议/互联网络协议)协议是Internet最基本的协议,简洁地说,就是由底层的IP协议和TCP协议组成的。在Internet没有形成之前,各个地方已经建立了很多小型的网络,称为局域网.Internet的中文意义是”网际网”,它实际上就是将全球各地的局域网连接起来而形成的一个”网之间的网(即网际网)"。然而,在连接之前的各式各样的局域网却存在不同的网络结构和数据传输规章,将这些小网连接起来后各网之间要通过什么样的规章来传输数据呢?这就象世界上有很多个国家,各个国家的人说各自的语言,世界上任意两个人要怎样才能相互沟通呢?如果全世界的人都能够说同一种语言(即世界语),这个问题不就解决了吗?TCP/IP协议正是Internet上的"世界语”。什么是udpﻩ用户数据报协议(UDP)是TCP/IP协议组的一个组成部分.它的创立是为了向应用程序供应一条访问IP的无连接功能的途径.TCP和UDP都使用IP。UDP的设计允许应用程序创建数据报,以及将它们编址到访问应用程序或进程的端口。UDP的基本作用就是向一个IP分组增加一个应用程序进程的端口地址。什么是MYSQLﻩMySQL是一种开放源代码的关系型数据库管理系统(RDBMS),MySQL数据库系统使用最常用的数据库管理语言—-结构化查询语言(SQL)进行数据库管理.由于MySQL是开放源代码的,因此任何人都可以在General

Public

License的许可下下载并依据共性化的需要对其进行修改。MySQL由于其速度、牢靠性和适应性而备受关注.大多数人都认为在不需要事务化处理的情况下,MySQL是管理内容最好的选择.MYSQL数据库配置ﻩLinux下mysql的安装步骤:第一步:筹备将mysql安装在/usr/local/stow/mysql中,下载了一个mysql-max-5。1.15-linux—i686-glibc23。tar.gz,复制到/usr/local/stow/,要记得用root登录,不然在这个名目是不能操作的。$gzip-dmysql-max-5.1。15—linux—i686-glibc23。tar.gz$tarxvfmysql—max-5。1.15-linux-i686-glibc23。tar$mvmysql—max—5.1。15-linux-i686-glibc23mysql-5.1现在已经将安装包解压,并重命名名目为mysql—5。1其次步:回到/usr/local下使用ln命令为mysql-5.1创建一个硬连接到/usr/local/mysql$ln-s/usr/local/stow/mysql-5.1/usr/local/mysql进入mysql-5.1名目:$cdmysql—5.1第三步:初始化mysql数据表。/scripts运行了mysql_install_db$cdmysql-5.1$./scripts/mysql_install_db第四步:给相关用户加上权限$chown-Rroot.$chown-Rmysqldata$chgrp-Rmysql.Mysql安装到此结束了,如果想启动数据库,可以进入/usr/local/mysql/bin执行下列命令启动服务:$。/mysqld_safe--user=mysql&关闭服务:$./mysqladmin-uroot—pshutdown修改root密码:$./mysqladmin—urootpassword'123456'进入数据库:$./mysql——user=root—-password=123456(密码可以不写,超级用户root是没有密码的)Linux下mysql与qt的驱动编译:QT与MYSQL的连接需要编译驱动,编译驱动需要QT的原码包,以下是步骤:第一步:进入QT原码文件夹(我们以QTDIR为代替),$cd$QTDIR/src/plugins/sqldrivers/mysql其次步:指定MYSQL的头文件与库,这里我们的MYSQL安装在/use/local/mysql-5.1$qmake—oMakefile"INCLUDEPATH+=/usr/local/mysql—5.1/include””LIBS+=-L/usr/local/mysql-5.1/lib-lmysqlclient_r”mysql.pro第三步:编译驱动$make$makeinstall以上3步后,QT连接MYSQL的驱动就编译完成了,直接被安装到QT的文件夹中,可以在QT中通过“ QSqlDatabasedb=QSqlDatabase::addDatabase(”QMYSQL");”语句调用数据库。MYSQL中涉及到中文字符校验的问题时,需要重新编译驱动,以下是步骤:第一步:需要修改原码中的src/sql/drivers/mysql/qsql_mysql。cpp文件。要修改的部分如下:第一百九十行的codec函数,注意红色加粗部分,是我修改增加的语句。taticQTextCodec*codec(MYSQL*mysql){returnQTextCodec::codecForName("GBK");//增加的部分#ifMYSQL_VERSION_ID〉=32321QTextCodec*heuristicCodec=QTextCodec::codecForName(mysql_character_set_name(mysql));if(heuristicCodec)returnheuristicCodec;#endifreturnQTextCodec::codecForLocale();}其次步:重新依据以上的编译驱动的方法编译驱动。再说明一点在数据库中建表的时候必须指定编码,这里我们使用gbk内部使用gbk_chinese_ci。本次项目内容介绍ﻩ本次项目采纳c\s构架实现linux下的即时通信,使用tcp,udp协议实现通信.本次项目主要分为:服务器端、客户端。服务器端主要包括数据库块和服务器块。客户端主要包括登陆块、主界面块和好友管理块。 需求分析目前,互联网络上的谈天通信软件种类繁多,例如,被大家熟知的QQ,ICO,MSN等。虽然网络上已经拥有如此多的谈天软件,但是,他们都是需要通过连接网络环境中使用,那么他们就不能担当重任了。为了满意这样一个网络环境的需要,我们开发出一款小巧的、能在局域网内正常通信(不连接外网)的谈天通信软件是必要的。程序效果的实现登陆主界面主界面—功能表主界面—群用户查询群查询单人谈天框群谈天框查询结果彩色字体程序文件介绍服务器 数据库块my_sql.h和close_sql.h:主要负责和数据库的连接,再qt调用sql语句实现数据库的调用。把需要的数据返回给服务器端。服务器块服务器主要由main.cpp、server.cpp、server.h、newClient.cpp、newClient。h、serverForm.h构成。main.cpp:程序主函数。server.cpp、server.h、serverForm.h:服务器端的可视窗口及其窗口内部按键操作。newClient。cpp、newClient。h:主要负责接受客户端信号并调用my_sql.h执行查询语句并把返回值返回给客户端。客户端登陆块xLogin.h,xLogin.cpp登陆的实现文件,传用户登陆信息(id,昵名)给主界面程序中。注册块xRegister.h,xRegister。cpp为用户注册程序,把注册信息发送给服务器,并接受返回服务器所发送的用户注册ID号xSelectIcon.h,xSelectIcon。cpp选择用户头像程序。主界面块xclient。h,client.cpp客户谈天的主界面,接受全部谈天信息并转发给相对应用户的谈天模式框,自动更新,群用户与私聊用户列表。设定用户在线状态。私聊块xchat.h,chat。cpp私聊框,接受所选其他好友谈天信息,并发送跟好友谈天信息,转变谈天信息,颜色,字体型号,粗,斜,下划线,截屏,发表情等。xselectface.h,selectface。cpp用户选择表情程序,并把所选图片信息传回谈天界面xdownload.h,download.cpp文件传输下载器,接受发送者ip以p2p的方式接受发送者发送的文件.*注:本版本只留有此接口暂不供应此服务。群聊块xteam.h,team。cpp组谈天框,供应转变谈天信息,颜色,字体型号,粗,斜,下划线,截屏,发表情等。xselectface1.h,selectface1.cpp用户选择表情程序,并把所选图片信息传回谈天界面.好友管理块xsearch。hxseach.cppxaddF。hxaddF.cppxseach.h与xaddF。h:用于声明xsearch.cpp,xaddF。cpp中用到的函数与变量,其中xsearch.cpp:实现客户端中可视化查询好友及群的窗口部件功能。xaddF.cpp:实现查找后将结果添加到好友列表功能. 程序代码详解服务器数据库块my_sql。h#include<QSqlDatabase>#include〈QSqlQuery〉#include〈QSqlQueryModel〉#include<QString>intlogin_system(QSqlQuery*query,intid,char*ip,char*pw)//登陆{ query—>exec(QString("SELECTCOUNT(*)AScntFROMusertableWHEREID=%1ANDPASSWORD='%2'")ﻩ ﻩ ﻩ。arg(id).arg(pw));ﻩquery—>next(); if(query—>value(0).toInt()==0)ﻩﻩreturn1;ﻩquery-〉exec(QString("selectONLINEfromusertablewhereID=%1").arg(id));ﻩquery-〉next();ﻩif(query—>value(0).toInt()==1)ﻩﻩreturn2;ﻩif(query—〉exec(QString("selectIPfromiptablewhereIP='%1'").arg(ip))) { ﻩif(query->next()) ﻩ return3;ﻩ elseif(query—>exec(QString(”SELECTNAME,PICFROMusertablewhereID=%1").arg(id)))ﻩ {ﻩﻩﻩquery—>exec(QString("INSERTINTOiptableVALUE('%1’,%2)").arg(ip).arg(id)); ﻩquery—〉exec(QString(”updateusertablesetONLINE=1whereID=%1")。arg(id)); ﻩquery-〉exec(QString(”SELECTNAME,PICFROMusertablewhereID=%1”)。arg(id)); ﻩreturn0;ﻩ }ﻩ else { return4;ﻩﻩ}ﻩ} elseﻩ return5;ﻩ}boolregister_user_date(QSqlQuery*query,char*pw,char*name,char*pic)//注册{ﻩif(query->exec(QString("INSERTINTOusertableVALUES(null,'%2',’%3',’%4’,0)”) ﻩ 。arg(pw)。arg(name)。arg(pic))) ﻩif(query->exec(QString("SELECTmax(ID)FROMusertable"))) ﻩ{ﻩﻩﻩreturntrue; ﻩ} else {ﻩ returnfalse; } elseﻩ{ﻩ returnfalse;ﻩ}}boolupdate_own_date(QSqlQuery*query,intid,char*name,char*pic)//修改个人资料{ﻩquery->exec(QString("updateusertableSETNAME=’%1',PIC=%2WHEREID=%3”)ﻩ ﻩ 。arg(name)。arg(pic).arg(id));ﻩquery->exec(QString("SELECTCOUNT(*)ASCNTFROMusertableWHEREID=%1ANDNAME=’%2'ANDPIC=’%3’")ﻩ ﻩ ﻩ。arg(id).arg(name)。arg(pic));ﻩquery-〉next();ﻩif(query-〉value(0)。toInt()>0) { returntrue; }ﻩelseﻩ{ returnfalse; }} ﻩboolupdate_password(QSqlQuery*query,intid,char*pw)//修改密码{ query->exec(QString(”UPDATEusertableSETPASSWORD='%1'WHEREID=%2") ﻩ ﻩ.arg(pw)。arg(id)); query-〉exec(QString("SELECTCOUNT(*)ascntFROMusertableWHEREID=%1ANDPASSWORD=’%2’")ﻩ ﻩﻩ .arg(id).arg(pw)); query-〉next(); if(query—>value(0)。toInt()>0)ﻩ{ ﻩreturntrue; }ﻩelseﻩ{ﻩﻩreturnfalse; }}ﻩﻩboolselect_friend_by_id(QSqlQuery*query,intid)//按ID查询好友{ returnquery—〉exec(QString(”SELECTID,NAME,ONLINE,PICFROMusertableWHEREID=%1") ﻩﻩ.arg(id));}ﻩ boolselect_friend_by_name(QSqlQuery*query,char*name)//按名字查询好友{ returnquery->exec(QString(”selectID,NAME,ONLINE,PICFROMusertableWHERENAME='%1’orderbyID") ﻩ ﻩ ﻩ.arg(name));}ﻩ boolselect_group_by_gid(QSqlQuery*query,intgid)//查询群号{ﻩreturnquery—>exec(QString(”SELECT*FROMgrouptableWHEREGID=%1")ﻩ ﻩ ﻩﻩﻩ.arg(gid));} ﻩbooldisplay_friends_from_group(QSqlQuery*query,intid)//显示群内好友{ returnquery->exec(QString("SELECTgrouptable.GID,grouptable.GNAME,usertable.ID,usertable。NAME,usertable.ONLINE,usertable.PICFROMgrouptable,usertable,gfriendtableWHEREusertable.ID=gfriendtable.IDandgfriendtable。GID=grouptable.GIDandgfriendtable。GIDIN(SELECTGIDFROMgfriendtableWHEREID=%1)ORDERBYGID,ONLINEdesc,ID”)ﻩ ﻩﻩ .arg(id));}booldisplay_friends_from_friendtable(QSqlQuery*query,intid)//显示个人好友列表{ returnquery—〉exec(QString(”SELECTID,NAME,ONLINE,PICFROMusertableWHEREusertable。IDin(selectFIDfromfriendtablewherefriendtable.ID=%1)orderbyONLINEdesc,ID") ﻩﻩﻩ ﻩﻩ.arg(id));}boolupdate_online(QSqlQuery*query,intid)//更新在线状态{ query-〉exec(QString("SELECTIDFROMusertableWHEREID=%1”)。arg(id));ﻩﻩif(query->next()) {ﻩ if(query—>exec(QString("updateusertablesetONLINE=0whereID=%1")。arg(id)))ﻩﻩ{ returntrue;ﻩ } ﻩelse ﻩ{ﻩﻩreturnfalse;ﻩﻩ}ﻩ}ﻩelse {ﻩﻩreturnfalse; }}intip_delete(QSqlQuery*query,char*ip)//IP清除{ﻩquery—〉exec(QString("SELECTcount(*)ascntFROMiptableWHEREIP=’%1'") .arg(ip)); if(query—>next())ﻩ{ if(query—>exec(QString("deletefromiptablewhereIP='%1’").arg(ip))) ﻩ{ ﻩprintf("lock\n");ﻩ returntrue;ﻩﻩﻩ} ﻩelse {ﻩ returnfalse; } } elseﻩ{ﻩ returnfalse; }}boolselect_ip_by_id(QSqlQuery*query,intid)//按ID查询IP{ query-〉exec(QString(”SELECTcount(*)ascntFROMiptableWHEREID=%1”)。arg(id));ﻩquery->next();ﻩif(query—〉value(0)。toInt()>0)ﻩ{ﻩ returnquery—>exec(QString("SELECT*FROMiptableWHEREID=%1")。arg(id)); ﻩ} else returnfalse;ﻩ}boolselect_down_by_id(QSqlQuery*query,intid)//查询下线人{ﻩif(query-〉exec(QString("SELECTID,NAME,PIC,ONLINEFROMusertableWHEREID=%1").arg(id)))ﻩ returntrue;ﻩelseﻩﻩreturnfalse;}intselect_online(QSqlQuery*query,intid)//查询在线状态{ inti=0; query-〉exec(QString(”selectONLINEfromusertablewhereID=%1").arg(id)); query—>first();ﻩwhile(query->next())ﻩ{ ﻩi++; } returni;}boolselect_ip_and_id(QSqlQuery*query)//按id查询IP{ﻩreturnquery-〉exec(QString("selectIP,IDfromiptable")); }boollogin_ip_id(QSqlQuery*query,intid,char*ip)//登陆ipid{ query->exec(QString("SELECTcount(*)ascntFROMusertableWHEREID=%1”).arg(id));ﻩquery-〉next(); if(query—〉value(0)。toInt()>0)ﻩ{ﻩ query->exec(QString("INSERTINTOiptableVALUE(’%1',%2)").arg(ip).arg(id));ﻩﻩquery—〉exec(QString(”updateusertablesetONLINE=1whereID=%1”)。arg(id));ﻩﻩreturntrue;ﻩ}ﻩelseﻩ{ ﻩreturnfalse;ﻩ}}boolid_find_ip(QSqlQuery*query,intid)//ip查询id{ﻩreturnquery—>exec(QString("selectIPfromiptablewhereID=%1”).arg(id));}booladd_friend(QSqlQuery*query,intid,intfid)//添加好友{ if(id==fid)ﻩ{ﻩﻩreturnfalse; }ﻩelseﻩ{ﻩ query-〉exec(QString(”SELECTCOUNT(*)ascntFROMfriendtableWHEREID=%1ANDFID='%2'”) ﻩ ﻩﻩ ﻩ.arg(id)。arg(fid));ﻩ query—〉next(); if(query—>value(0).toInt()>0) ﻩ{ ﻩreturnfalse; } ﻩelseﻩﻩ{ ﻩﻩquery—>exec(QString("INSERTINTOfriendtableVALUES(%1,%2)”) ﻩﻩﻩ。arg(id)。arg(fid)); ﻩ returntrue;ﻩﻩ}ﻩ}}booladd_group(QSqlQuery*query,intgid,intid)//添加群{ﻩquery-〉exec(QString(”SELECTCOUNT(*)ascntFROMgfriendtableWHEREGID=%1ANDID=’%2’") ﻩ .arg(gid).arg(id));ﻩquery-〉next();ﻩif(query->value(0)。toInt()>0) { returnfalse; } elseﻩ{ ﻩquery—>exec(QString("INSERTINTOgfriendtableVALUES(%1,%2)")ﻩﻩﻩ ﻩ ﻩﻩ.arg(gid)。arg(id));ﻩﻩreturntrue;ﻩ}}close_sql.h#include<QSqlDatabase>#include<QSqlQuery>#include<QSqlQueryModel>#include〈QString>voiddelete_ip_user(QSqlQuery*query)//清空ip表设置全部用户为下线服务器退出时使用{ﻩquery—>exec(QString("rollback")); query->exec(QString("updateusertablesetONLINE=0"));ﻩquery—〉exec(QString("deletefromiptable"));}voidkick_user(QSqlQuery*query,intid)//设置指定用户下线{ﻩquery-〉exec(QString("deletefromiptablewhereID=%1”)。arg(id)); query—>exec(QString("updateusertablesetONLINE=0whereID=%1").arg(id));}voidkick_all(QSqlQuery*query)//设置全部用户下线{ﻩquery—>exec(QString(”deletefromiptable”));ﻩquery-〉exec(QString(”updateusertablesetONLINE=0"));}服务器块main。cpp#include〈QApplication>#include<QSqlDatabase>#include<QSqlQuery〉#include〈QTextCodec>#include”server.h”intmain(intargc,char**argv){ﻩQApplicationa(argc,argv);ﻩQTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK”));//使用gbk编码ﻩQTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK"));ﻩQSqlDatabasedb=QSqlDatabase::addDatabase("QMYSQL”);//数据库使用MYSQL数据库 db.setHostName("localhost");//设定主机登陆ﻩdb。setDatabaseName(”qq”);//设定数据库名称 db。open();//打开数据库ﻩQSqlQueryquery;ﻩquery.exec(”SETNAMES'gbk'");//数据库使用gbk编码 Serverserver;ﻩserver。show();ﻩreturna.exec();}server.cpp#include<QTreeWidgetItem〉#include<QSqlQuery>#include”newClient.h”#include"server.h"#include”close_sql。h"Server::Server(){ setupUi(this);ﻩcreateActions(); createTrayIcon();ﻩtcpSocket=newQTcpSocket(this);ﻩtcpServer=newQTcpServer(this);ﻩudpSocket=newQUdpSocket(this);ﻩtcpPort=60001;//设置tcp端口 udpPort=45454;//设置udp端口 kickAllUser(); udpSocket-〉bind(udpPort);ﻩtcpServer—〉listen(QHostAddress::Any,tcpPort);ﻩconnect(tcpServer,SIGNAL(newConnection()),this,SLOT(newConnectionSlot()));ﻩconnect(pushButton_close,SIGNAL(clicked()),qApp,SLOT(quit())); connect(treeWidget,SIGNAL(clicked(constQModelIndex&)),this,SLOT(kickReady(constQModelIndex&))); connect(pushButton_kick,SIGNAL(clicked()),this,SLOT(kickUser())); connect(pushButton_kickall,SIGNAL(clicked()),this,SLOT(kickAllUser()));}Server::~Server(){ﻩQSqlQuery*query=newQSqlQuery();ﻩdelete_ip_user(query);}voidServer::newConnectionSlot()//连接槽{ newClient*newclient=newnewClient(tcpServer,udpSocket); connect(newclient,SIGNAL(userActionInform(QStringList)),this,SLOT(refreshList(QStringList)));}voidServer::refreshList(QStringListuserDetails)//返回用户信息并打印{ intonline=userDetails.at(3).toInt();ﻩQTreeWidgetItem*treeWidgetItem;ﻩif(online==1)ﻩ{ﻩﻩtreeWidgetItem=newQTreeWidgetItem();ﻩﻩtreeWidgetItem—>setText(0,userDetails.at(0));ﻩ treeWidgetItem->setText(1,userDetails.at(1)); ﻩtreeWidgetItem-〉setText(2,userDetails.at(2));ﻩ treeWidget—〉addTopLevelItem(treeWidgetItem);ﻩ label_total-〉setNum(label_total->text().toInt()+1); label_result—〉setText(QString("%1%2%3").arg("User”).arg(userDetails。at(0)) ﻩﻩﻩ ﻩ.arg("hasloggedin!")); ﻩpushButton_kickall-〉setEnabled(true); }elseﻩ{ for(inti=0;i<treeWidget->topLevelItemCount();i++)ﻩ {ﻩﻩﻩtreeWidgetItem=treeWidget-〉topLevelItem(i);ﻩﻩﻩif(userDetails.at(0).toInt()==treeWidgetItem->text(0)。toInt())ﻩﻩ { ﻩtreeWidget->takeTopLevelItem(i);treeWidgetItem=newQTreeWidgetItem();ﻩ ﻩlabel_total—〉setNum(label_total-〉text().toInt()-1); ﻩ ﻩlabel_result->setText(QString(”%1%2%3”)。arg("User").arg(userDetails。at(0))ﻩﻩﻩﻩ ﻩ。arg("hasloggedout!")); if(treeWidget->topLevelItemCount()==0)ﻩﻩ ﻩ{ ﻩﻩ ﻩpushButton_kick—>setEnabled(false);ﻩﻩﻩﻩﻩpushButton_kickall—〉setEnabled(false);ﻩ } ﻩbreak;ﻩ ﻩ}ﻩ }ﻩ}ﻩtreeWidget—>sortByColumn(0,Qt::AscendingOrder);}voidServer::kickReady(constQModelIndex&modelIndex){ row=modelIndex。row();ﻩpushButton_kick->setEnabled(true); pushButton_kickall->setEnabled(true);}voidServer::createActions()//创建托盘的菜单{ minimizeAction=newQAction(tr(”Minimize"),this); connect(minimizeAction,SIGNAL(triggered()),this,SLOT(hide()));ﻩrestoreAction=newQAction(tr("Restore”),this); connect(restoreAction,SIGNAL(triggered()),this,SLOT(show())); quitAction=newQAction(tr(”Quit"),this);ﻩconnect(quitAction,SIGNAL(triggered()),qApp,SLOT(quit()));}voidServer::createTrayIcon()//创建托盘的图标{ menu=newQMenu(this); menu—>addAction(minimizeAction);ﻩmenu->addAction(restoreAction);ﻩmenu-〉addSeparator(); menu->addAction(quitAction);ﻩicon=newQSystemTrayIcon(QIcon("INTERNET9.png”),this); icon—〉setContextMenu(menu);ﻩicon->show(); setWindowIcon(QIcon("INTERNET9.png"));}voidServer::closeEvent(QCloseEvent*){ﻩqApp->quit();}voidServer::kickUser()//踢掉指定用户{ﻩQSqlQuery*query=newQSqlQuery();ﻩkick_user(query,treeWidget-〉topLevelItem(row)—〉text(0).toInt());ﻩlabel_result->setText(QString("%1%2%3").arg("User”).arg(treeWidget—>topLevelItem(row)—>text(0)。toInt()) ﻩﻩ 。arg("hasbeenkicked!"));ﻩtreeWidget-〉takeTopLevelItem(row); if(treeWidget->topLevelItemCount()==0)ﻩ{ﻩ pushButton_kick->setEnabled(false); pushButton_kickall->setEnabled(false); } label_total->setNum(label_total->text()。toInt()-1);}voidServer::kickAllUser()//踢掉全部用户{ﻩQSqlQuery*query=newQSqlQuery();ﻩkick_all(query); label_result—>setText(QString("%1")。arg("Alluserhasbeenkicked!")); treeWidget—>clear();ﻩpushButton_kick-〉setEnabled(false);ﻩpushButton_kickall->setEnabled(false);ﻩlabel_total->setNum(0);}server.h//server。cpp中的函数在本函数中定义#include<QTcpSocket>#include<QTcpServer〉#include〈QUdpSocket〉#include〈QString〉#include〈QWidget>#include<QSystemTrayIcon>#include<QAction〉#include<QMenu>#include<QWidget>#include<QModelIndex〉#include”serverForm。h"classServer:publicQWidget,publicUi::Form{ﻩQ_OBJECTprivate: QTcpSocket*tcpSocket; QTcpServer*tcpServer; QUdpSocket*udpSocket;ﻩquint16tcpPort; quint16udpPort;ﻩQSystemTrayIcon*icon; QAction*minimizeAction; QAction*restoreAction; QAction*quitAction;ﻩQMenu*menu; QModelIndexmodelIndex;ﻩQTreeWidgetItem*selectTreeWidgetItem; introw;ﻩvirtualvoidcloseEvent(QCloseEvent*);public:ﻩServer(); ~Server();privateslots: voidnewConnectionSlot(); voidrefreshList(QStringList);ﻩvoidcreateActions();ﻩvoidcreateTrayIcon(); voidkickReady(constQModelIndex&);ﻩvoidkickUser(); voidkickAllUser();};newClient。cpp#include〈QMessageBox〉#include〈QSqlQuery>#include<QString>#include<QStringList>#include<QByteArray〉#include<QVariant>#include〈QTextCodec>#include"newClient。h”#include”my_sql.h"newClient::newClient(QTcpServer*tcpServer,QUdpSocket*udpSocket){ﻩudpPort=45454;ﻩtcpPort=54545;ﻩtcpSocket=newQTcpSocket(this);ﻩthis-〉tcpServer=tcpServer;ﻩthis->udpSocket=udpSocket;ﻩtcpSocket=tcpServer—>nextPendingConnection(); connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(getMessage()));}newClient::~newClient(){}voidnewClient::getMessage(){ﻩin.setVersion(QDataStream::Qt_4_2); in。setDevice(tcpSocket);ﻩin〉〉flag_Receive; switch(flag_Receive)ﻩ{ﻩ case0:registerNewAccount();//用户注册ﻩ ﻩ break; ﻩcase1:loginRequest();//登陆恳求 ﻩﻩ break; case2:searchUserByNickname();//昵称搜寻用户 ﻩﻩ break; ﻩcase3:searchUserByID();id搜寻用户 ﻩﻩﻩbreak; ﻩcase4:searchGroup();//搜寻组 ﻩ break; ﻩcase5:refreshLoginList();//登陆时刷新好友列表ﻩ ﻩbreak; ﻩcase6:groupMessageTransmission();//组消息传送 ﻩ break;ﻩ case7:privateMessageTransmission();//个人消息传送ﻩﻩﻩ break;ﻩ ﻩcase10:addFriendRequest();//添加好友申请ﻩﻩ ﻩbreak; /* case11://agreeFriendAdded(in);ﻩ ﻩ break; case12:getFriendsStatus(in);ﻩﻩ break;*/ case13:addGroupRequest();//添加组好友申请ﻩ break;ﻩ case14:exitInform();//退出通知 ﻩﻩﻩbreak;ﻩ case15:relogin();//再上线ﻩ ﻩ break;ﻩﻩdefault:break;ﻩ}}/*ﻩregisteranewaccount*/voidnewClient::registerNewAccount(){ﻩQSqlQuery*query=newQSqlQuery();ﻩcharpw[16],name[16],pic[16];ﻩQStringnewName;ﻩQStringnewPassword; in〉>newName>〉newPassword>>iconName; strcpy(pw,(constchar*)newPassword.toLocal8Bit());ﻩstrcpy(name,(constchar*)newName.toLocal8Bit()); strcpy(pic,(constchar*)iconName.toLocal8Bit());ﻩif(register_user_date(query,pw,name,pic)) ﻩflag_Send=0; else ﻩflag_Send=1; sendRegisterResult(query);}/*ﻩreturnregisterresult*/voidnewClient::sendRegisterResult(QSqlQuery*query){ QByteArrayblock; QDataStreamout(&block,QIODevice::WriteOnly);ﻩout.setVersion(QDataStream::Qt_4_2);ﻩout.setDevice(tcpSocket);ﻩout〈〈(quint16)0<〈flag_Send; if(flag_Send==0)ﻩ{ﻩ query->next(); ﻩnewID=query—>value(0).toString(); out〈<newID; } out。device()-〉seek(0);ﻩout<<(quint16)(block.size()—sizeof(quint16));}/* loginaaccount*/voidnewClient::loginRequest(){ QSqlQuery*query=newQSqlQuery(); charpw[16],ipAddress[16]; QStringpassword; in>>senderID〉>password>>clientAddress;ﻩuserID=senderID.toInt();ﻩstrcpy(pw,(constchar*)password.toLocal8Bit()); strcpy(ipAddress,(constchar*)clientAddress。toLocal8Bit()); switch(login_system(query,userID,ipAddress,pw)) { case0:flag_Send=0;ﻩﻩ ﻩbreak;ﻩ case1:flag_Send=1; ﻩ break; ﻩcase2:flag_Send=3;ﻩﻩﻩﻩbreak; case3:flag_Send=4;ﻩ ﻩ break;ﻩ case4:flag_Send=2;ﻩﻩﻩ break; ﻩdefault:flag_Send=5; } sendLoginResult(query);ﻩ}/* returnloginresult*/voidnewClient::sendLoginResult(QSqlQuery*query){ﻩQTextCodec::setCodecForLocale(QTextCodec::codecForName(”GBK")); QTextCodec::setCodecForCStrings(QTextCodec::codecForName(”GBK")); QTextCodec*codec=QTextCodec::codecForName("GBK”);ﻩQByteArrayblock;ﻩQDataStreamout(&block,QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_2);ﻩout。setDevice(tcpSocket);ﻩout<〈(quint16)0<<flag_Send;ﻩif(query->next()) { ﻩnickName=codec-〉fromUnicode(query—>value(0).toString());ﻩﻩiconName=query->value(1).toString(); ﻩout<〈nickName<<iconName;ﻩ}ﻩ QMessageBox::warning(this,tr(”MyApplication”),nickName); out.device()—>seek(0); out〈〈(quint16)(block.size()-sizeof(quint16));}/* searchuserbynickname*/voidnewClient::searchUserByNickname(){ QSqlQuery*query=newQSqlQuery();ﻩcharname[16];ﻩin〉〉nickName;ﻩstrcpy(name,(constchar*)nickName.toLocal8Bit()); if(select_friend_by_name(query,name)) ﻩflag_Send=15; elseﻩﻩflag_Send=1; sendSearchUserResult(query);}/*ﻩsearchuserbyID*/voidnewClient::searchUserByID(){ QSqlQuery*query=newQSqlQuery(); QStringsearchUserID; in>>searchUserID; userID=searchUserID.toInt(); if(select_friend_by_id(query,userID)) flag_Send=15;ﻩelseﻩﻩflag_Send=1;ﻩsendSearchUserResult(query);}/* sendsearchuserresulttoclient*/voidnewClient::sendSearchUserResult(QSqlQuery*query){ﻩQTextCodec*codec=QTextCodec::codecForName("GBK");ﻩQByteArrayblock;ﻩQDataStreamout(&block,QIODevice::WriteOnly);ﻩout.setVersion(QDataStream::Qt_4_2);ﻩout。setDevice(tcpSocket); out〈〈(quint16)0〈<flag_Send;ﻩif(flag_Send==15) {ﻩ QStringListidList;ﻩ QStringListnicknameList; ﻩQStringListstatusList; intcount=0;ﻩ while(query->next())ﻩﻩ{ ﻩ count++;ﻩ idList。append(query—>value(0).toString()); ﻩ nicknameList。append(codec—〉fromUnicode(query->value(1).toString())); ﻩﻩstatusList.append(query—〉value(2).toString()); ﻩ} ﻩout〈<count〈〈idList〈<nicknameList〈<statusList;ﻩ} out。device()->seek(0);ﻩout<〈(quint16)(block.size()—sizeof(quint16));}/*ﻩsearchgrouponlybyIDcurrently*/voidnewClient::searchGroup(){ QSqlQuery*query=newQSqlQuery();ﻩQStringsearchGroupID; in〉>searchGroupID; groupID=searchGroupID。toInt();ﻩif(select_group_by_gid(query,groupID))ﻩﻩflag_Send=16;ﻩelseﻩ flag_Send=1; sendSearchGroupResult(query);}voidnewClient::sendSearchGroupResult(QSqlQuery*query){ﻩQTextCodec*codec=QTextCodec::codecForName("GBK"); QStringgroupID,groupName; QByteArrayblock; QDataStreamout(&block,QIODevice::WriteOnly);ﻩout。setVersion(QDataStream::Qt_4_2); out。setDevice(tcpSocket);ﻩout〈<(quint16)0〈<flag_Send;ﻩif(flag_Send==16)ﻩ{ ﻩquery->next(); ﻩgroupID=query->value(0)。toString();ﻩ groupName=codec->fromUnicode(query-〉value(1).toString()); out<〈groupID<<groupName; }ﻩout.device()—>seek(0);ﻩout<〈(quint16)(block.size()-sizeof(quint16));}voidnewClient::addFriendRequest(){ QSqlQuery*query=newQSqlQuery(); in〉〉senderID>〉addgroupID; userID=senderID.toInt(); friendID=addgroupID.toInt();ﻩif(add_friend(query,userID,friendID)) flag_Send=10; else ﻩflag_Send=11;ﻩsendaddFriendRequest();}voidnewClient::sendaddFriendRequest(){ﻩQByteArrayblock; QDataStreamout(&block,QIODevice::WriteOnly); out。setVersion(QDataStream::Qt_4_2); out.setDevice(tcpSocket); out<<(quint16)0〈<flag_Send; out.device()—>seek(0);ﻩout〈<(quint16)(block。size()-sizeof(quint16)); QSqlQuery*userQuery=newQSqlQuery();ﻩif(display_friends_from_friendtable(userQuery,userID)) flag_Send=6;ﻩelse flag_Send=1;ﻩsendLoginFriendResult(userQuery);}/*ﻩaddgroup*/voidnewClient::addGroupRequest(){ﻩQSqlQuery*query=newQSqlQuery(); in〉〉addgroupID>〉senderID;ﻩuserID=senderID.toInt();ﻩgroupID=addgroupID。toInt(); if(add_group(query,groupID,userID)) ﻩflag_Send=12; else ﻩflag_Send=11;ﻩsendaddGroupRequest();}voidnewClient::sendaddGroupRequest(){ QByteArrayblock;ﻩQDataStreamout(&block,QIODevice::WriteOnly);ﻩout.setVersion(QDataStream::Qt_4_2); out.setDevice(tcpSocket); out<〈(quint16)0〈<flag_Send; out.device()->seek(0);ﻩout<<(quint16)(block。size()-sizeof(quint16)); QSqlQuery*userQuery=newQSqlQuery(); if(display_friends_from_friendtable(userQuery,userID)) flag_Send=6; elseﻩ flag_Send=1; sendLoginFriendResult(userQuery);}/*ﻩgiveeverynewloginerhisfriends'statusandgroups’salso*/voidnewClient::refreshLoginList(){ﻩQSqlQuery*userQuery=newQSqlQuery(); in>>userID;ﻩif(display_friends_from_friendtable(userQuery,userID)) ﻩflag_Send=6;ﻩelseﻩ flag_Send=1; ﻩsendLoginFriendResult(userQuery);}voidnewClient::sendLoginFriendResult(QSqlQuery*userQuery){ QTextCodec*codec=QTextCodec::codecForName("GBK");ﻩQByteArraydatagram; QDataStreamout(&datagram,QIODevice::WriteOnly);ﻩout<<flag_Send<〈userID;ﻩif(flag_Send==6)ﻩ{ QStringListidList; ﻩQStringListnicknameList; QStringListstatusList; ﻩQStringListpicList;ﻩ intcount=0;ﻩ while(userQuery->next()) { ﻩcount++; ﻩﻩidList.append(userQuery—>value(0).toString()); nicknameList。append(codec->fromUnicode(userQuery-〉value(1).toString())); ﻩﻩstatusList.append(userQuery-〉value(2)。toString()); picList.append(userQuery->value(3).toString());ﻩ }ﻩﻩout<〈count〈<idList〈<nicknameList<<statusList〈<picList;ﻩ} udpSocket—>writeDatagram(datagram。data(),qint64(datagram。size()),ﻩ ﻩQHostAddress::Broadcast,udpPort); QSqlQuery*groupQuery=newQSqlQuery();ﻩif(display_friends_from_group(groupQuery,userID))ﻩﻩflag_Send=7; else flag_Send=1;ﻩ sendLoginGroupResult(groupQuery);}voidnewClient::sendLoginGroupResult(QSqlQuery*groupQuery){ QTextCodec*codec=QTextCodec::codecForName(”GBK"); QByteArraydatagram; QDataStreamout(&datagram,QIODevic

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论