代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
局域网QQ群聊软件
作者:
Sbright
/ 发布于
2012/7/23
/
542
局域网QQ群聊软件
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); this->setWindowTitle("群聊服务器"); this->tcpServer = new QTcpServer; this->db = new SqliteDb(this); this->db->clearLogStat(); this->userRefresh(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_listenButton_clicked() { QString ip; QString port; ip.clear(); port.clear(); ip = ui->ip_lineEdit->text().trimmed(); port = ui->port_lineEdit->text().trimmed(); if("开始监听"==ui->listenButton->text()) { QRegExp rxIp("\\d+\\.\\d+\\.\\d+\\.\\d+"); QRegExp rxPort(("[1-9]\\d{3,4}")); rxIp.setPatternSyntax(QRegExp::RegExp); rxPort.setPatternSyntax(QRegExp::RegExp); if ( !rxPort.exactMatch(port) || !rxIp.exactMatch(ip) ) { QMessageBox::critical( NULL, tr("提示"), tr("请输入正确的IP和端口.") ); } else { if(!this->tcpServer->listen(QHostAddress(ip),(quint16)port.toInt())) { QMessageBox::critical(this,tr("错误"),tr("TCP监听失败: %1").arg(this->tcpServer->errorString())); } else { QObject::connect(this->tcpServer,SIGNAL(newConnection()),this,SLOT(tcpConnectionSlot())); this->udpSocket = new QUdpSocket(this); if(!this->udpSocket->bind(QHostAddress(ip),(quint16)port.toUInt()+1)) { QMessageBox::critical(this,tr("错误"),tr("UDP监听失败:%1").arg(this->udpSocket->errorString())); } else { QObject::connect(this->udpSocket,SIGNAL(readyRead()),this,SLOT(readUdpMsgSlot())); ui->listenButton->setText("断开监听"); ui->ip_lineEdit->setEnabled(false); ui->port_lineEdit->setEnabled(false); } } } } else if("断开监听"==ui->listenButton->text()) { this->db->clearLogStat(); ui->listenButton->setText(tr("开始监听")); this->tcpServer->close(); ui->ip_lineEdit->setEnabled(true); ui->port_lineEdit->setEnabled(true); } } void MainWindow::tcpConnectionSlot() { tcpSocket = new QTcpSocket; tcpSocket = this->tcpServer->nextPendingConnection(); QObject::connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(tcpMegSlot())); } void MainWindow::tcpMegSlot() { QByteArray block=this->tcpSocket->readAll(); QString name; QString ip; QString port; quint16 msgSize; QString msgType; QDataStream in(&block,QIODevice::ReadOnly); in>>msgSize>>msgType; if("MSG_USER_LOGIN" == msgType) { in>>name; this->db->getUserInfo(name); QByteArray sendMsg; QDataStream out(&sendMsg,QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_6); if(!this->db->strListUser.isEmpty()) { msgType="MSG_LOGIN_SUCCESS"; this->db->updateUserLogStat(name,"1"); this->userRefresh(); } else { in>>ip>>port; msgType="MSG_LOGIN_SUCCESS"; this->db->insertNewUser(name,ip,port); this->userRefresh(); } out<<quint16(0)<<msgType; out.device()->seek(0); out<<quint16(block.size()-sizeof(quint16)); this->tcpSocket->write(sendMsg); this->tcpSocket->disconnectFromHost(); } } void MainWindow::readUdpMsgSlot() { while(this->udpSocket->hasPendingDatagrams()) { QByteArray block; QHostAddress senderIp; quint16 senderPort; block.resize(this->udpSocket->pendingDatagramSize()); if(-1==this->udpSocket->readDatagram(block.data(),block.size(),&senderIp,&senderPort)) continue; qDebug()<<senderIp.toString()<<" "<<QString::number(senderPort); this->processData(block,senderIp.toString(),QString::number(senderPort)); } } void MainWindow::processData(QByteArray block, QString ip, QString port) { QDataStream in(&block,QIODevice::ReadOnly); quint16 dataGramSize; QString msgType; in >> dataGramSize >> msgType; if("MSG_USER_LOGOUT"==msgType) { QString Name; in >> Name; this->db->updateUserLogStat(Name,"0"); this->userRefresh(); this->db->getUsrAllOnline(); QStringList nameList = this->db->strListName; QString msgType = "GET_USER_LIST"; QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_6); out << (quint16)0 << msgType<< nameList; out.device()->seek(0); out << (quint16)(block.size() - sizeof(quint16)); for(int i=0;i<this->db->strListIp.count();i++) { QString port_send=this->db->strListPort.at(i); this->udpSocket->writeDatagram(block.data(),block.size(),QHostAddress(this->db->strListIp.at(i)),port_send.toInt()); } } else if ( "GET_USER_LIST" == msgType ) { QString name; in >> name; if ( !name.isEmpty() ) { this->db->updateUserIpAndPort(name,ip,"6666"); this->userRefresh(); } this->db->getUsrAllOnline(); QStringList nameList = this->db->strListName; QString msgType = "GET_USER_LIST"; QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_6); out << (quint16)0 << msgType<< nameList; out.device()->seek(0); out << (quint16)(block.size() - sizeof(quint16)); if(!this->udpSocket->writeDatagram(block.data(),block.size(),QHostAddress(ip),6666)) { QMessageBox::critical(NULL, tr("提示"),tr("!udpSocket->writeDatagram.") ); } } else if( "MSG_CLIENT_CHAT" == msgType) { QString fromName,buffer,port_send; in >>fromName>>buffer; this->db->updateUserIpAndPort(fromName,ip,"6666"); this->userRefresh(); this->displayText(fromName,buffer); QByteArray blockTosend; QDataStream tosend(&blockTosend,QIODevice::WriteOnly); QString mytype="MSG_CLIENT_CHAT"; tosend<<(quint16)0<<mytype<<fromName<<buffer; tosend.device()->seek(0); tosend << (quint16)(blockTosend.size() - sizeof(quint16)); this->db->getUsrAllOnline(); for(int i=0;i<this->db->strListIp.count();i++) { port_send=this->db->strListPort.at(i); this->udpSocket->writeDatagram(blockTosend.data(),blockTosend.size(),QHostAddress(this->db->strListIp.at(i)),port_send.toInt()); } } } void MainWindow::displayText(QString peerName, QString msg) { QString message=peerName+" : "+msg+"\n"; ui->recordlistWidget->addItem(message); } void MainWindow::userRefresh() { model=new QSqlQueryModel; this->db->connectDB(); model->setQuery(tr("SELECT name,logstat,ip,port FROM information")); //ui->all_user_on_table->setModel(model); ui->tableView->setModel(model); model->setHeaderData(0,Qt::Horizontal,tr("昵称")); model->setHeaderData(1,Qt::Horizontal,tr("状态")); model->setHeaderData(2,Qt::Horizontal,tr("IP地址")); model->setHeaderData(3,Qt::Horizontal,tr("端口号")); ui->tableView->setColumnWidth(0, 80); ui->tableView->setColumnWidth(1, 50); ui->tableView->setColumnWidth(2, 140); ui->tableView->setColumnWidth(3, 80); ui->tableView->show(); this->db->closeDB(); } </div>
试试其它关键字
局域网QQ群聊软件
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Sbright
贡献的其它代码
(
1
)
.
局域网QQ群聊软件
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3