这个是别人的一个C++、qt环境下的C/S聊天器,我稍微改了一下:
注:如果在本机上运行服务端和客户端。则在客户端上只需输入127.0.0.1即可
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4704 2006-09-28 01:43 sourcechat.cpp
文件 1142 2006-09-27 22:56 sourcechat.h
文件 6636 2010-05-26 16:42 sourcedebugBuildLog.htm
文件 169931 2010-05-26 16:39 sourcedebugchat.obj
文件 137674 2010-05-26 16:39 sourcedebuglogin.obj
文件 71125 2010-05-26 16:39 sourcedebugmain.obj
文件 3308 2010-05-26 16:38 sourcedebugmoc_chat.cpp
文件 76581 2010-05-26 16:39 sourcedebugmoc_chat.obj
文件 3258 2010-05-26 16:38 sourcedebugmoc_login.cpp
文件 58482 2010-05-26 16:39 sourcedebugmoc_login.obj
文件 67 2010-05-26 16:42 sourcedebugmt.dep
文件 103936 2010-05-26 16:42 sourcedebugsource.exe
文件 878 2010-05-26 16:42 sourcedebugsource.exe.intermediate.manifest
文件 1426808 2010-05-26 16:42 sourcedebugsource.ilk
文件 1534976 2010-05-26 16:42 sourcedebugsource.pdb
文件 643 2006-09-21 21:46 sourcelogin.cpp
文件 446 2006-09-21 21:42 sourcelogin.h
文件 6545 2006-09-22 16:09 sourcelogin_ui.h
文件 234 2006-09-21 21:38 sourcemain.cpp
文件 6688 2010-05-26 16:42 sourceMakefile
文件 7162 2010-05-26 16:42 sourceMakefile.Debug
文件 7246 2010-05-26 16:42 sourceMakefile.Release
文件 2829 2006-09-25 22:40 sourcemoc_chat.cpp
文件 2786 2006-09-25 21:42 sourcemoc_login.cpp
文件 7605248 2010-05-26 16:48 sourcesource.ncb
文件 391 2010-05-26 16:41 sourcesource.pro
文件 877 2010-05-26 16:38 sourcesource.sln
..A..H. 13312 2010-05-26 16:48 sourcesource.suo
文件 10953 2010-05-26 16:42 sourcesource.vcproj
文件 1495 2010-05-26 16:48 sourcesource.vcproj.lei-PC.lei.user
............此处省略9个文件信息
#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(“
[%1] %2 %3
%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 sourcechat.cpp
文件 1142 2006-09-27 22:56 sourcechat.h
文件 6636 2010-05-26 16:42 sourcedebugBuildLog.htm
文件 169931 2010-05-26 16:39 sourcedebugchat.obj
文件 137674 2010-05-26 16:39 sourcedebuglogin.obj
文件 71125 2010-05-26 16:39 sourcedebugmain.obj
文件 3308 2010-05-26 16:38 sourcedebugmoc_chat.cpp
文件 76581 2010-05-26 16:39 sourcedebugmoc_chat.obj
文件 3258 2010-05-26 16:38 sourcedebugmoc_login.cpp
文件 58482 2010-05-26 16:39 sourcedebugmoc_login.obj
文件 67 2010-05-26 16:42 sourcedebugmt.dep
文件 103936 2010-05-26 16:42 sourcedebugsource.exe
文件 878 2010-05-26 16:42 sourcedebugsource.exe.intermediate.manifest
文件 1426808 2010-05-26 16:42 sourcedebugsource.ilk
文件 1534976 2010-05-26 16:42 sourcedebugsource.pdb
文件 643 2006-09-21 21:46 sourcelogin.cpp
文件 446 2006-09-21 21:42 sourcelogin.h
文件 6545 2006-09-22 16:09 sourcelogin_ui.h
文件 234 2006-09-21 21:38 sourcemain.cpp
文件 6688 2010-05-26 16:42 sourceMakefile
文件 7162 2010-05-26 16:42 sourceMakefile.Debug
文件 7246 2010-05-26 16:42 sourceMakefile.Release
文件 2829 2006-09-25 22:40 sourcemoc_chat.cpp
文件 2786 2006-09-25 21:42 sourcemoc_login.cpp
文件 7605248 2010-05-26 16:48 sourcesource.ncb
文件 391 2010-05-26 16:41 sourcesource.pro
文件 877 2010-05-26 16:38 sourcesource.sln
..A..H. 13312 2010-05-26 16:48 sourcesource.suo
文件 10953 2010-05-26 16:42 sourcesource.vcproj
文件 1495 2010-05-26 16:48 sourcesource.vcproj.lei-PC.lei.user
............此处省略9个文件信息
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)