資源簡介
這個是別人的一個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?<?? 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?<?????out?<}
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 圖數據結構 最短路徑
評論
共有 條評論