Linux课程设计Word下载.docx
《Linux课程设计Word下载.docx》由会员分享,可在线阅读,更多相关《Linux课程设计Word下载.docx(10页珍藏版)》请在冰豆网上搜索。
数据包通用格式定义如下
#defineMAX_UDP_SIZE1000
structudp_packet{
inttype;
intsenderId;
longsize;
charcontent[MAX_UDP_SIZE];
};
type:
表示该数据包的类型,直接决定content字段的含义
senderId:
该数据包的发送者的ID,
size:
整个数据包的数据长度
content:
数据包的内容,其数据格式由type决定。
服务器和客户端接受到数据包后,根据type字段的值来解析content字段的数据,从而作出正确的处理和响应。
所有的数据包类型以及对应的content字段的数据结构全部定义在define.h文件中
由上图可以看出,服务器和客户端程序在总体结构上相似。
服务器和客户端的全部功在数据处理模块中实现,这也是整个程序的核心之处
由于数据包的接受在单独的线程中完成,而界面采用Qt实现。
因此在接受线程中采用QCoreApplication:
:
postEvent()方法向界面线程发送通知事件,在界面窗口中通过重载customEvent()方法响应该通知,然后从Server中获取数据并显示。
整个过程中涉及到线程同步和多线程安全问题,觉采用信号量和互斥量解决。
四、文件操作
聊天记录的保存是通过文件的操作实现的
具体代码
voidChatClient:
saveRecord(){
saveRecords("
chatRecords.txt"
);
}
saveRecords(constQString&
fileName){
QFilefile(fileName);
if(!
file.open(QFile:
WriteOnly|QFile:
Text)){
QMessageBox:
warning(this,tr("
Application"
),
tr("
Cannotwritefile%1:
\n%2."
)
.arg(fileName).arg(file.errorString()));
return;
}
QTextStreamout(&
file);
out<
<
chatBox->
toPlainText();
五、源程序:
#include<
QtGui>
QtNetwork>
#include"
chatclient.h"
ChatClient:
ChatClient(QWidget*parent)
:
QWidget(parent){
QStringgbkText;
//中文化
leftLayout=newQVBoxLayout;
leftTopLayout=newQHBoxLayout;
gbkText=gbkText.fromLocal8Bit("
聊天室IP:
"
hostIpLabel=newQLabel(gbkText);
hostIpBox=newQLineEdit(tr("
127.0.0.1"
));
端口:
hostPortLabel=newQLabel(gbkText);
hostPortBox=newQLineEdit(tr("
7654"
昵称:
userLabel=newQLabel(gbkText);
userBox=newQLineEdit;
连接"
connectBtn=newQPushButton(gbkText);
leftTopLayout->
addWidget(hostIpLabel);
addWidget(hostIpBox);
addWidget(hostPortLabel);
addWidget(hostPortBox);
addWidget(userLabel);
addWidget(userBox);
addWidget(connectBtn);
//左上结束
chatBox=newQTextEdit;
setReadOnly(true);
欢迎使用简单聊天室-v0.2--JasonLee@fzu"
setText(gbkText);
//左中
sendLineLayout=newQHBoxLayout;
关闭"
closeBtn=newQPushButton(gbkText);
发送"
sendBtn=newQPushButton(gbkText);
sendLineLayout->
addWidget(closeBtn);
addWidget(sendBtn);
leftBottomLayout=newQVBoxLayout;
msgBox=newQLineEdit;
leftBottomLayout->
addWidget(msgBox);
addLayout(sendLineLayout);
//左下
leftLayout->
addLayout(leftTopLayout);
addWidget(chatBox);
addLayout(leftBottomLayout);
//左边栏
rightLayout=newQVBoxLayout;
公告栏\n"
newsBox=newQTextEdit;
newsBox->
append(gbkText);
Linux实验三"
rightLayout->
addWidget(newsBox);
历史用户列表:
\n"
userListBox=newQListWidget;
userListBox->
addItem(gbkText);
addWidget(userListBox);
//右栏结束
midLayout=newQHBoxLayout;
midLayout->
addLayout(leftLayout);
addLayout(rightLayout);
topLayout=newQVBoxLayout;
//菜单栏
menuBar=newQMenuBar();
fileMenu=menuBar->
addMenu(tr("
&
File"
保存聊天记录"
saveAct=fileMenu->
addAction(tr("
save"
connect(saveAct,SIGNAL(triggered()),this,SLOT(saveRecord()));
topLayout->
addWidget(menuBar);
bottomLayout=newQHBoxLayout;
//如状态栏等
mainLayout=newQVBoxLayout;
mainLayout->
addLayout(topLayout);
addLayout(midLayout);
addLayout(bottomLayout);
setLayout(mainLayout);
connect(connectBtn,SIGNAL(clicked()),this,SLOT(enterChatroom()));
connect(sendBtn,SIGNAL(clicked()),this,SLOT(sendMsg()));
connect(closeBtn,SIGNAL(clicked()),this,SLOT(close()));
~ChatClient(){
closeEvent(QCloseEvent*event){//关闭窗口时调用
QStringgbkTitle,gbkContent;
gbkTitle=gbkTitle.fromLocal8Bit("
提醒:
gbkContent=gbkContent.fromLocal8Bit("
您确定关闭简单聊天室?
StandardButtonret;
ret=QMessageBox:
warning(this,gbkTitle,gbkContent,
Close|QMessageBox:
Cancel);
if(ret==QMessageBox:
Cancel)event->
ignore();
enterChatroom(){
if(this->
hostIpBox->
text()=="
||this->
hostPortBox->
||
this->
userBox->
){
聊天室信息不完整!
warning(this,gbkTitle,gbkContent);
hostAddr=this->
text();
port=this->
text().toInt();
nickname=this->
tcpSocket=newQTcpSocket(this);
tcpSocket->
connectToHost(hostAddr,port);
connectBtn->
setEnabled(false);
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(receiveMsg()));
///////
sendMsg(){
msgBox->
信息不能为空!
QStringmsg=this->
text()+"
"
+this->
write(msg.toLocal8Bit(),msg.length());
msgBox->
clear();
receiveMsg(){
while(tcpSocket->
bytesAvailable()>
0){
QByteArraydatagram;
datagram.resize(tcpSocket->
bytesAvailable());
read(datagram.data(),datagram.size());
QStringmsg;
msg=msg.fromLocal8Bit(datagram.data());
append(msg);
五、测试与调试
六、总结
对于此次课程设计,我早就在开课之前借了Linux相关书籍参看,但上面讲的主要是有关Linux操作方面的内容,编程方面讲得很少,而且在开课之前也并不知道课设的题目是什么,因此此次课设基本上都是在开学后的这两周内完成的。
以前做过的软件方面的课设如C语言课设、数据结构课设都是在假期完成的,由于自己是一个十分追求完美的人,因此几乎每次都花了将近大半个假期的时间来做,如C语言就花了一个多月的时间来做,分数当然也较高,有90来分。
对于课程设计,我历来都是相当认真的,此次操作系统的课程设计当然也不例外。
可是Linux以前没怎么接触过,学校也没怎么系统地讲过,在刚接到题目时除了知道如何用GCC编译等等,几乎可以算作处于一无所知的状态。
时间紧任务重,要从对Linux一无所知的状态到独立出色地完成课设,这是件不容易的事情啊!
但最后,我终于明白,分数不过是个数字,知识才是自己的。
通过这次课程设计,我确实学到了很多东西,多年后我可能已经忘记这次课设最后打了多少分,但这些学到的东西却可以使我受益终生。
除了知识技术上的东西,我更锻炼了自己的快速学习能力;
我学会了如何快速有效地从图书馆、网络获取自己需要的信息;
我尝到了在周围很多同学拷来拷去时孤军奋战的痛苦;
我体会了夜以继日完成一个项目时中途过程的艰辛及最终完成后巨大的成就感……我更加深了人生的信心,以后面对任何一个困难的项目,我想我都不会惧怕,并最终能够成功地将其完成。
感谢老师,感谢此次课程设计。
虽然在其中吃了不少苦头,但我毫不后悔,因为我满载而归。