资源简介
这个是别人的一个C++、qt环境下的C/S聊天器,我稍微改了一下:
注:如果在本机上运行服务端和客户端。则在客户端上只需输入127.0.0.1即可

代码片段和文件信息
#include “chat.h“
Chat::Chat()
{
serverSocket = NULL;
server = NULL;
client = NULL;
setupUi(this);
login = new LoginDialog;
login->show();
connect(login SIGNAL(showChatWindow()) this SLOT(showAndHideSlot()));
connect(login SIGNAL(sendEnterMessage(QString QString))
thisSLOT(enterSlot(QString QString )));
connect(writeMessageEdit SIGNAL(textChanged()) this SLOT(changeButtonStateSlot()));
connect(aboutButton SIGNAL(clicked()) this SLOT(createAboutSlot()));
connect(sendButton SIGNAL(clicked()) this SLOT(appendMessageSlot()));
connect(quitButton SIGNAL(clicked()) qApp SLOT(quit()));
}
void Chat::showAndHideSlot()
{
delete login;
this->show();
}
void Chat::createAboutSlot()
{}
void Chat::enterSlot(QString name QString host)
{
port = 22222;
if (host.isEmpty())
{
socketServer();
}
else
{
socketClient(host);
}
userName = name;
onlineMessageList->addItem(name);
}
void Chat::changeButtonStateSlot()
{
bool boo_dis = writeMessageEdit->toPlainText().isEmpty();
sendButton->setDisabled(boo_dis);
}
void Chat::appendMessageSlot()
{
QString content = writeMessageEdit->toPlainText();
if (content.isEmpty())
{
QMessageBox::warning(this “出错“ QString::fromLocal8Bit(“发送的内容不能为空“));
return;
}
nowDateTime = QDateTime::currentDateTime();
showMessageEdit->append(
QString(“\n[%1] %2 %3\n%4“)
.arg(nowDateTime.toString(“yyyy-MM-dd hh:mm:ss“))
.arg(userName)
.arg(QString::fromLocal8Bit(“说道:“))
.arg(content) );
QDataStream out;
if (serverBool)
{
out.setDevice(serverSocket);
}
else if (!serverBool)
{
out.setDevice(client);
}
int mark = 0;
out << mark;
out << userName << content;
writeMessageEdit->clear();
}
void Chat::socketServer()
{
serverBool = true;
server = new QTcpServer(this);
serverSocket = new QTcpSocket(this);
server->listen(QHostAddress::Any port);
connect(server SIGNAL(newConnection()) this SLOT(newConnectionSlot()));
}
void Chat::newConnectionSlot()
{
serverSocket = server->nextPendingConnection();
connect(serverSocket SIGNAL(readyRead()) this SLOT(newDataSlot()));
connect(serverSocket SIGNAL(disconnected()) this SLOT(deleNameSlot()));
}
void Chat::socketClient(QString host)
{
serverBool = false;
client = new QTcpSocket(this);
client->connectToHost(host port);
connect(client SIGNAL(connected()) this SLOT(addSlot()));
connect(client SIGNAL(readyRead()) this SLOT(newDataSlot()));
connect(client SIGNAL(disconnected()) this SLOT(deleNameSlot()));
}
void Chat::addSlot()
{
QString myName = userName;
QString content;
QDataStream out(client);
int mark = 11;
out << mark;
out << myName << content;
}
void Chat::newDataSlot()
{
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4704 2006-09-28 01:43 source\chat.cpp
文件 1142 2006-09-27 22:56 source\chat.h
文件 6636 2010-05-26 16:42 source\debug\BuildLog.htm
文件 169931 2010-05-26 16:39 source\debug\chat.obj
文件 137674 2010-05-26 16:39 source\debug\login.obj
文件 71125 2010-05-26 16:39 source\debug\main.obj
文件 3308 2010-05-26 16:38 source\debug\moc_chat.cpp
文件 76581 2010-05-26 16:39 source\debug\moc_chat.obj
文件 3258 2010-05-26 16:38 source\debug\moc_login.cpp
文件 58482 2010-05-26 16:39 source\debug\moc_login.obj
文件 67 2010-05-26 16:42 source\debug\mt.dep
文件 103936 2010-05-26 16:42 source\debug\source.exe
文件 878 2010-05-26 16:42 source\debug\source.exe.intermediate.manifest
文件 1426808 2010-05-26 16:42 source\debug\source.ilk
文件 1534976 2010-05-26 16:42 source\debug\source.pdb
文件 643 2006-09-21 21:46 source\login.cpp
文件 446 2006-09-21 21:42 source\login.h
文件 6545 2006-09-22 16:09 source\login_ui.h
文件 234 2006-09-21 21:38 source\main.cpp
文件 6688 2010-05-26 16:42 source\Makefile
文件 7162 2010-05-26 16:42 source\Makefile.Debug
文件 7246 2010-05-26 16:42 source\Makefile.Release
文件 2829 2006-09-25 22:40 source\moc_chat.cpp
文件 2786 2006-09-25 21:42 source\moc_login.cpp
文件 7605248 2010-05-26 16:48 source\source.ncb
文件 391 2010-05-26 16:41 source\source.pro
文件 877 2010-05-26 16:38 source\source.sln
..A..H. 13312 2010-05-26 16:48 source\source.suo
文件 10953 2010-05-26 16:42 source\source.vcproj
文件 1495 2010-05-26 16:48 source\source.vcproj.lei-PC.lei.user
............此处省略9个文件信息
- 上一篇:16C++MFC课程设计之吹泡泡游戏
- 下一篇:MFC 图数据结构 最短路径
相关资源
- qTox (基于 peer-to-peer )
- 国际象棋的qt源代码
- C++中头文件与源文件的作用详解
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- 利用C++哈希表的方法实现电话号码查
- 移木块游戏,可以自编自玩,vc6.0编写
- C++纯文字DOS超小RPG游戏
- VC++MFC小游戏实例教程(实例)+MFC类库
- 连铸温度场计算程序(C++)
- 6自由度机器人运动学正反解C++程序
- Em算法(使用C++编写)
- libstdc++-4.4.7-4.el6.i686.rpm
- VC++实现CMD命令执行与获得返回信息
- 白话C++(全)
- C++标准库第1、2
- 大数类c++大数类
- C++语言编写串口调试助手
- c++素数筛选法
- QT上位机
- qt媒体播放器
- QT5开发及源代码
- qt完整项目
- C++ mqtt 用法
- 商品库存管理系统 C++ MFC
- qt进度条(RoundProgressBar)
- Qt的纽带风格界面实现(Office Ribbon风
- qt 实现画板
- QT Hisi demo
- 基础qt数据库读取和显示
评论
共有 条评论