面向图书管理系统 TCP协议 LINUX平台.docx
- 文档编号:24305043
- 上传时间:2023-05-26
- 格式:DOCX
- 页数:14
- 大小:307.05KB
面向图书管理系统 TCP协议 LINUX平台.docx
《面向图书管理系统 TCP协议 LINUX平台.docx》由会员分享,可在线阅读,更多相关《面向图书管理系统 TCP协议 LINUX平台.docx(14页珍藏版)》请在冰豆网上搜索。
面向图书管理系统TCP协议LINUX平台
成都信息工程学院
课程设计报告
面向连接的图书信息查询系统
课程名称:
高级程序设计语言B
学生姓名:
学生学号:
专业班级:
任课教师:
附件:
成绩评价表
成绩评定表
程序编译和功能演示(30%)
编程代码质量(10%)
编程水平与程序设计能力(30%)
程序设计说明书(论文)撰写质量(30%)
总分
目录
1.程序设计3
2.程序功能模块详细设计(源代码+程序说明)3
3.程序功能演示和测试结果4
结论4
1.程序设计
1.1程序设计目标
根据套接字编程知识,采用面向连接TCP协议,实现服务器端存储图书基本信息,用户可通过客户端输入书名或者出版社,或者价格或者出版时间,查询到书的基本情况。
1)客户端可以连接服务器端,服务器端显示连接客户端连接信息(即客户端IP地址),客户端显示连接成功或者失败信息;
2)客户端输入书名,然后发送给服务器,服务器则查询是否有该书,若有则服务器把该书的基本信息(包括书名,出版社,出版时间,价格)相关信息返回给客户端,客户端收到,则显示出来
3)客户端输入出版社,然后发送给服务器,服务器则查询是否符合出版社条件的书籍,若有则服务器把找到符合出版社名称书的基本信息全部发送给客户端(包括书名,出版社,出版时间,价格)相关信息返回给客户端,客户端收到,则显示出来
4)与第3不类似,输入价格或者出版时间,则服务器端把符合此价格或者出版时间的书结果返回客户端,则显示出来;
5)在客户端输入“quit”,服务器端终止该客户端连接,客户端程序结束。
1.2程序运行环境
*******************
linux操作系统编译工具:
gcc
1.3程序功能模块设计
*******************
2.程序功能模块详细设计(源代码+程序说明)
2.1服务器端
********************
#include
#include
#include
#include
#include
#include
#include
#include
#include
#definePORT6666
#defineBUFFERSIZE1024
structBOOK
{
charMING[30];
charchuban[30];
charcash[30];
chartime[30];
}stu[11]={{"name:
C","from:
Qinghuacolleg","cost:
11","time:
2012.1.3"},{"name:
DIAN","from:
BeiJin","cost:
35","time:
2003,2,3"},{"name:
SHUZHI","from:
CUIT","cost:
99","time:
2008.1.2"},{"name:
AAA","from:
NB","cost:
23","time:
1999.2.3"},{"name:
BBB","from:
NIKE","cost:
1","time:
0034.2.3"},{"name:
DDD","from:
WWW","cost:
12","time:
2012.3.4"},{"name:
FFF","from:
EWF","cost:
39","time:
2006.3.4"},{"name:
SSS","from:
ASD","cost:
33","time:
2004.3.4"},{"name:
QQQ","from:
MM","cost:
33","time:
2013.2.4"},{"name:
Q1989","from:
FG","cost:
12","time:
1999.9.3"},{"name:
ZZZ","from:
WWW","cost:
11","time:
2999.3.5"}};
intmain()
{
intsockfd,newfd;
structsockaddr_inmy_addr;
structsockaddr_intheir_addr;
unsignedintsin_size,listnum;
listnum=0;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("socketerror\n");
exit(-1);
}
bzero(&my_addr,sizeof(my_addr));
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(PORT);
my_addr.sin_addr.s_addr=htonl(INADDR_ANY);
//bzero(&my_addr.sin_zero,0);
if(bind(sockfd,(structsockaddr*)&my_addr,sizeof(structsockaddr))==-1)
{
printf("binderror\n");
exit(-1);
}
if(listen(sockfd,listnum)==-1)
{
printf("listenerror\n");
exit(-1);
}
sin_size=sizeof(structsockaddr_in);
while
(1)
{
if((newfd=accept(sockfd,(structsockaddr*)&their_addr,&sin_size))==-1)
{
printf("accepterror\n");
exit(-1);
}
printf("linkstar\n");
send(newfd,"welcometomyserver\n",22,0);//
printf("yougetaconnectionfrom%s\n",
inet_ntoa(their_addr.sin_addr));
charsock_buf[BUFFERSIZE];
bzero(sock_buf,BUFFERSIZE);
intrval;
if((rval=recv(newfd,sock_buf,BUFFERSIZE,0))==-1)
{
printf("Recverror\n");
exit(-1);
}
elseif(!
strcmp(sock_buf,"quit"))
{
break;
send(newfd,"break!
",BUFFERSIZE,0);
}
else
{
//printf("find:
%s\n",sock_buf);
inti=0;
for(i=0;i<11;i++)
{
if(strstr(sock_buf,stu[i].MING)!
=0)
{
char*pbuf=sock_buf;
memset(sock_buf,0,sizeof(sock_buf));
strncpy(pbuf,stu[i].MING,sizeof(stu[i].MING));
pbuf+=sizeof(stu[i].MING);
strncpy(pbuf,stu[i].chuban,sizeof(stu[i].chuban));
pbuf+=sizeof(stu[i].chuban);
strncpy(pbuf,stu[i].cash,sizeof(stu[i].cash));
pbuf+=sizeof(stu[i].cash);
strncpy(pbuf,stu[i].time,sizeof(stu[i].time));
pbuf+=sizeof(stu[i].time);
printf("findbook:
%s\n",stu[i].MING);
//printf("%s\n",stu[i].chuban);
//printf("%s\n",stu[i].cash);
//printf("%s\n",stu[i].time);
send(newfd,sock_buf,sizeof(sock_buf),0);
pbuf=sock_buf;
break;
}
if(strstr(sock_buf,stu[i].chuban)!
=0)
{
char*pbuf1=sock_buf;
memset(sock_buf,0,sizeof(sock_buf));
strncpy(pbuf1,stu[i].chuban,sizeof(stu[i].chuban));
pbuf1+=sizeof(stu[i].chuban);
strncpy(pbuf1,stu[i].MING,sizeof(stu[i].MING));
pbuf1+=sizeof(stu[i].MING);
strncpy(pbuf1,stu[i].cash,sizeof(stu[i].cash));
pbuf1+=sizeof(stu[i].cash);
strncpy(pbuf1,stu[i].time,sizeof(stu[i].time));
pbuf1+=sizeof(stu[i].time);
printf("findbook:
%s\n",stu[i].MING);
//printf("%s\n",stu[i].chuban);
//printf("%s\n",stu[i].cash);
//printf("%s\n",stu[i].time);
send(newfd,sock_buf,sizeof(sock_buf),0);
pbuf1=sock_buf;
break;
}
if(strstr(sock_buf,stu[i].cash)!
=0)
{
char*pbuf2=sock_buf;
memset(sock_buf,0,sizeof(sock_buf));
strncpy(pbuf2,stu[i].cash,sizeof(stu[i].cash));
pbuf2+=sizeof(stu[i].cash);
strncpy(pbuf2,stu[i].MING,sizeof(stu[i].MING));
pbuf2+=sizeof(stu[i].MING);
strncpy(pbuf2,stu[i].chuban,sizeof(stu[i].chuban));
pbuf2+=sizeof(stu[i].chuban);
strncpy(pbuf2,stu[i].time,sizeof(stu[i].time));
pbuf2+=sizeof(stu[i].time);
printf("findbook:
%s\n",stu[i].MING);
send(newfd,sock_buf,sizeof(sock_buf),0);
pbuf2=sock_buf;
break;
}
if(strstr(sock_buf,stu[i].time)!
=0)
{
char*pbuf2=sock_buf;
memset(sock_buf,0,sizeof(sock_buf));
strncpy(pbuf2,stu[i].time,sizeof(stu[i].time));
pbuf2+=sizeof(stu[i].time);
strncpy(pbuf2,stu[i].cash,sizeof(stu[i].cash));
pbuf2+=sizeof(stu[i].cash);
strncpy(pbuf2,stu[i].MING,sizeof(stu[i].MING));
pbuf2+=sizeof(stu[i].MING);
strncpy(pbuf2,stu[i].chuban,sizeof(stu[i].chuban));
pbuf2+=sizeof(stu[i].chuban);
printf("findbook:
%s\n",stu[i].MING);
send(newfd,sock_buf,sizeof(sock_buf),0);
pbuf2=sock_buf;
break;
}
}
if(i>11)
{
send(newfd,"can'tfind\n",BUFFERSIZE,0);
printf("can'tfind\n");
break;
}
close(newfd);
close(sockfd);
}
}
}
2.2客户端
********************
#include
#include
#include
#include
#include
#include
#include
#include
#include
#definePORT6666
//#defineDEF_IP"127.0.0.1"
intmain(intargc,char*argv[])
{
intsockfd,zzm;
charbuffer[1024];
structsockaddr_inserver_addr;
structhostent*host;
intportnumber,nbytes;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("socketERROR\n");
exit
(1);
}
if(argc!
=2)
{
printf("Usage:
%s
exit(-1);
}
if((host=gethostbyname(argv[1]))==NULL)
{
perror("gethostbynameerror.");
exit
(1);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
perror("Createsocketfailed.");
exit
(1);
}
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(PORT);
//server_addr.sin_addr.s_addr=inet_addr(DEF_IP);
server_addr.sin_addr=*((structin_addr*)host->h_addr);
if(connect(sockfd,(structsockaddr*)&server_addr,sizeof(structsockaddr))==-1)
{
printf("connect澶辫触\n");
exit
(1);
}
if(((zzm=recv(sockfd,buffer,1024,0))==-1))
{
perror("recverror.");
exit
(1);
}
printf("ServerMessage:
%s\n",buffer);
while
(1)
{
printf("find:
\n");
scanf("%s",buffer);
send(sockfd,buffer,1024,0);
intrval=0;
memset(buffer,0,sizeof(buffer));
if((rval=recv(sockfd,buffer,1024,0))<0)
{
printf("Recverror\n");
exit
(1);
}
else
{
char*pbuf=buffer;
printf("Theend:
%s\n",pbuf);
pbuf+=30;
printf("%s\n",pbuf);
pbuf+=30;
printf("%s\n",pbuf);
pbuf+=30;
printf("%s\n",pbuf);
break;
}
}
}
3.程序功能演示和测试结果
*********************
结论
***************************************
本程序实现了设计的所以要求,运行流畅功能正常。
完成了本次程序设计的要求。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向图书管理系统 TCP协议 LINUX平台 面向 图书 管理 系统 TCP 协议 LINUX 平台