Linux聊天室系统期末大作业_第1页
Linux聊天室系统期末大作业_第2页
Linux聊天室系统期末大作业_第3页
Linux聊天室系统期末大作业_第4页
Linux聊天室系统期末大作业_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1 NANCHANG UNIVERSITY Linux 系统编程系统编程 课程设计课程设计 题题 目目 Linux 聊天室报告 学学 院 院 软件学院 专专 业 业 计算机软件 班班 级 级 计软 121 班 姓姓 名 名 李俊楠 起讫日期 起讫日期 2014 12 22 2015 1 10 任课教师 任课教师 陈悦 完成时间 完成时间 2015 年 1 月 10 日前 填表日期 填表日期 2015 年 1 月 6 日 需求分析 2 目目 录录 1 需求分析需求分析 3 1 1 项目介绍 50 分 3 1 2 功能需求 3 2 系统分析系统分析 3 2 1 本程序需解决的有关技术问题 4 2 2 程序流程 6 3 系统测试系统测试 8 3 1 运行截图 8 3 2 程序代码 4 小结 小结 1 5 参考文献 参考文献 1 需求分析 3 一 需求分析一 需求分析 1 1 项目介绍 1 1 1 介绍 本设计主要实现一个 Linux 下的局域网聊天工具的设计 该设计主要分为两部分 客 户端部分和服务器部分 设计并实现一个简单的聊天室程序 可以是终端字符界面 支持 用户管理 用户名 密码注册和登录 客户端登陆到服务器聊天后 可以在聊天室内与其他 用户交流 这个聊天室中的任何一个用户输入一段字符后 室内的其他用户都可以看到这 句话 据此 聊天程序分为客户端和服务器端 客户端对应每一个参加聊天的用户 完成 从终端上输入采集并传递到服务器端和从服务器端接收信息输出显示的功能 1 1 2 程序应满足如下要求 1 必须出现多线程 2 程序中要具备文件读取和写入的操作 3 在程序中必须使用网络通信进行信息传输 4 要求程序具备简单的菜单功能 根据用户的输入 执行相应的操作 在菜单界 面 必须显示程序的名称 版本信息 开发者的名字 班级 学号等信息 1 2 功能需求 1 客户端输入服务器 IP 和端口号 2 服务器成功启动 3 每个客户端可以找到服务器 并可以与服务器进行连接 4 客户端与服务器可以实现通讯 需求分析 4 二 系统分析二 系统分析 2 1 本程序需解决的有关技术问题 2 1 1 字符串的判断字符串的判断 写一个脚本 检测用户输入串的合法性 要求串由大小写字母 数字组成 无标点 特殊符号 空格 思路 将输入的串中 非字母数字的内容删除或替换为空后若和原来输入的相同 则合法 否则不合法 bin bash isvalidAlphaNum compressed echo 1 sed s alnum g 替换不合要求字符为空 if compressed 1 then return 1 not valide else return 0 valide fi 函数体 判断字串的合法性 echo n Enter input n 强制不换行 read input 读取内容存到 input 中 无 符号 if isvalidAlphaNum then 加引号 否则可能会出错 echo invalide exit 1 else echo valide fi exit 0 需求分析 5 运行截图 运行截图 2 1 2 月份的截取转换月份的截取转换 输入一个 month day year 格式的日期串 程序将处理 month 为 其英 文单词的前三个字母 第一个字母大写 其余的小写 思路 首先判断 month 是数字 还是单词 若是数字 则可查 数字 month 映射表 自定义 case 结构 若是单词 则取前三个字母 并格式化 bin bash monthnoToName case 1 in 1 month Jan 2 month Feb 3 month Mar 4 month Apr 5 month May 6 month Jun 7 month Jul 8 month Aug 9 month Sep 10 month Oct 11 month Nov 12 month Dec echo 0 Unknown numeric month value 1 exit 1 default esac return 0 if ne 3 then 若参数个数不为 3 则有误 echo Usage 0 month day year exit 1 需求分析 6 fi 判断参数 1 是数字还是单词 方法 替换串中所有数字 若结果为空 则为数 字 否则为单词 if n echo 1 sed s digit g n 检测串是为空 then monthnoToName 1 数字 else 取第一个字母并将其变为大写 再将结果与取出的第二三字母组合 month echo 1 cut c1 tr lower upper month month echo 1 cut c2 3 tr upper lower fi echo month 2 3 输出结果 exit 0 运行截图 2 2 程序流程 1 连接服务器 连接服务器 连接服务器之前需要解析服务器地址 创建套接字 设置相关参数 需求分析 7 2 发送消息功能 发送消息功能 需求分析 8 3 3 服务器功能模块 服务器功能模块 结束 系统设计与实现 9 3 系统测试系统测试 3 1 运行截图运行截图 服务器端运行截图 客户端登陆 输入昵称和密码 文件的写入 系统设计与实现 10 加入两名聊天人 聊天信息 双方接收 系统设计与实现 11 3 2 程序代码程序代码 1 客户端代码 客户端代码 系统设计与实现 12 include include include include include include include include define TRUE 1 define PORT 1000 static int sockfd void recvfromserver 接受服务器消息线程入口函数 char mes 1024 int nbytes 0 while 1 memset mes 0 sizeof mes nbytes read sockfd mes sizeof mes if nbytes 0 mes nbytes 0 printf s n mes pthread exit NULL int main int argc char argv int sockfd char buffer 1024 系统设计与实现 13 struct sockaddr in server addr struct hostent host int portnumber nbytes char strhost 127 0 0 1 char clientname 20 char password 20 char mes 1024 int thr id thread ID for the newly created thread pthread t p thread thread s structure if argc 1 fprintf stderr Usage s a n argv 0 exit 1 if host gethostbyname strhost NULL fprintf stderr Gethostname error n exit 1 客户程序开始建立 sockfd 描述符 printf Creating the Set interface n if sockfd socket AF INET SOCK STREAM 0 1 fprintf stderr Socket Error s a n strerror errno exit 1 客户程序填充服务端的资料 bzero server addr sin family AF INET server addr sin port htons PORT server addr sin addr struct in addr host h addr printf The successful landing nWelcome to zhe chat room n 客户程序发起连接请求 系统设计与实现 14 if connect sockfd struct sockaddr exit 1 连接成功了 printf The successful landing nWelcome to zhe chat room n printf Please enter your nickname n scanf s clientname printf Please enter your password n scanf s password printf nThe successful landing write sockfd clientname sizeof clientname printf nNow you can chat with others Quit CUT DOWN LANDING n n thr id pthread create while 1 memset buffer 0 sizeof buffer memset mes 0 sizeof mes scanf s buffer strcat mes clientname strcat mes strcat mes buffer printf main thread s n mes if write sockfd mes sizeof mes 1 fprintf stderr Write Error s n strerror errno exit 1 if strcmp buffer Quit 0 系统设计与实现 15 break 结束通讯 close sockfd exit 0 2 服务器代码 服务器代码 include include include include include include include include define MAXLINE 1000 在一条消息中最大的输出字符数 define LISTENQ 20 最大监听队列 define PORT 1000 监听端口 define MAXFD 20 最大的在线用户数量 void get client void int sockfd i static int maxi 0 maxi 表示当前 client 数组中最大的用户的 i 值 static int client MAXFD void recvandsend void 监听转发线程入口函数 int index 0 int nbytes 0 char buffer 1024 int len int outindex 0 while 1 系统设计与实现 16 if maxi 0 memset buffer 0 sizeof buffer nbytes 0 index nbytes read client index buffer sizeof buffer printf d d n index client index if nbytes 0 buffer nbytes 0 printf s n buffer outindex 0 while outindex maxi index 0 pthread exit NULL int main int argc char argv int client fd LISTENQ clientnum 0 struct sockaddr in server addr struct sockaddr in client addr int sin size portnumber char hello Hello Are You Fine n int thr id thread ID for the newly created thread pthread t p thread thread s structure 系统设计与实现 17 int new fd 0 memset client 0 sizeof client if argc 1 fprintf stderr Usage s portnumber a n argv 0 exit 1 服务器端开始建立 socket 描述符 if sockfd socket AF INET SOCK STREAM 0 1 fprintf stderr Socket error s n a strerror errno exit 1 服务器端填充 sockaddr 结构 bzero server addr sin family AF INET server addr sin addr s addr htonl INADDR ANY server addr sin port htons PORT 捆绑 sockfd 描述符 if bind sockfd struct sockaddr exit 1 printf 服务器监听端口 d n PORT 监听 sockfd 描述符 if listen sockfd LISTENQ 1 fprintf stderr Listen error s n a strerror errno exit 1 thr id pthread create printf NAME Li Junnan No 8000612030 Class Ji ruan121 n printf Welcome to the chat room n 系统设计与实现 18 while 1 服务器阻塞 直到客户程序建立连接 if maxi 20 printf Over the max people n continue sin siz

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论