linu网络编程课程设计.docx
《linu网络编程课程设计.docx》由会员分享,可在线阅读,更多相关《linu网络编程课程设计.docx(15页珍藏版)》请在冰豆网上搜索。
linu网络编程课程设计
《Linux网络编程》
课程设计
班级:
姓名:
指导老师:
一、设计背景
Linux 操作系统作为一个开源的操作系统被越来越多的人所应用,它的好处在于操作系统源代码的公开化!
只要是基于GNU公约的软件你都可以任意使用并修改它的源代码。
通过这次课程设计能更好的学习网络编程知识和掌握LINUX平台上应用程序设计开发的过程,将大学四年所学知识综合运用,为未来的工作学习打下基础。
二、设计目的
1、学习epoll跟FTP被动模式
2、掌握linux基本命令,例如ls、cd、login;
3、学会如何编译、运行
三、环境要求
1、centos64位操作系统
2、gcc编译器
四、设计原理
4.1客户端
客户端程序的主要任务有以下3个:
(1)、分析用户输入的命令。
(2)、根据命令向服务器发出请求
(3)、接受服务器返回请求的结果
客户端为用户提供了3种命令:
(1)、get:
从服务器下载文件
(2)、list:
列出客户端当前目录的内容(3)、quit离开
4.2服务器端
(1)、分析请求代码。
(2)、根据请求代码做相应的处理
(3)、等待返回结果或者应答信息
五、软件测试结果
六、部分主代码
#include"ftserve.h"
intmain(intargc,char*argv[])
{
intsock_listen,sock_control,port,pid;
if(argc!
=2){
printf("usage:
./ftserveport\n");
exit(0);
}
port=atoi(argv[1]);
//createsocket
if((sock_listen=socket_create(port))<0){
perror("Errorcreatingsocket");
exit
(1);
}
while
(1){//waitforclientrequest
//createnewsocketforcontrolconnection
if((sock_control=socket_accept(sock_listen))<0)
break;
//createchildprocesstodoactualfiletransfer
if((pid=fork())<0){
perror("Errorforkingchildprocess");
}elseif(pid==0){
close(sock_listen);
ftserve_process(sock_control);
close(sock_control);
exit(0);
}
close(sock_control);
}
close(sock_listen);
return0;
}
/**
*Sendfilespecifiedinfilenameoverdataconnection,sending
*controlmessageovercontrolconnection
*Handlescaseofnullorinvalidfilename
*/
voidftserve_retr(intsock_control,intsock_data,char*filename)
{
FILE*fd=NULL;
chardata[MAXSIZE];
size_tnum_read;
fd=fopen(filename,"r");
if(!
fd){
//senderrorcode(550Requestedactionnottaken)
send_response(sock_control,550);
}else{
//sendokay(150Filestatusokay)
send_response(sock_control,150);
do{
num_read=fread(data,1,MAXSIZE,fd);
if(num_read<0){
printf("errorinfread()\n");
}
//sendblock
if(send(sock_data,data,num_read,0)<0)
perror("errorsendingfile\n");
}while(num_read>0);
//sendmessage:
226:
closingconn,filetransfersuccessful
send_response(sock_control,226);
fclose(fd);
}
}
/**
*Sendlistoffilesincurrentdirectory
*overdataconnection
*Return-1onerror,0onsuccess
*/
intftserve_list(intsock_data,intsock_control)
{
chardata[MAXSIZE];
size_tnum_read;
FILE*fd;
intrs=system("ls-l|tail-n+2>tmp.txt");
if(rs<0){
exit
(1);
}
fd=fopen("tmp.txt","r");
if(!
fd){
exit
(1);
}
/*Seektothebeginningofthefile*/
fseek(fd,SEEK_SET,0);
send_response(sock_control,1);//starting
memset(data,0,MAXSIZE);
while((num_read=fread(data,1,MAXSIZE,fd))>0){
if(send(sock_data,data,num_read,0)<0){
perror("err");
}
memset(data,0,MAXSIZE);
}
fclose(fd);
send_response(sock_control,226);//send226
return0;
}
/**
*Opendataconnectiontoclient
*Returns:
socketfordataconnection
*or-1onerror
*/
intftserve_start_data_conn(intsock_control)
{
charbuf[1024];
intwait,sock_data;
//Waitforgo-aheadoncontrolconn
if(recv(sock_control,&wait,sizeofwait,0)<0){
perror("Errorwhilewaiting");
return-1;
}
//Getclientaddress
structsockaddr_inclient_addr;
socklen_tlen=sizeofclient_addr;
getpeername(sock_control,(structsockaddr*)&client_addr,&len);
inet_ntop(AF_INET,&client_addr.sin_addr,buf,sizeof(buf));
//Initiatedataconnectionwithclient
if((sock_data=socket_connect(CLIENT_PORT_ID,buf))<0)
return-1;
returnsock_data;
}
/**
*Authenticateauser'scredentials
*Return1ifauthenticated,0ifnot
*/
intftserve_check_user(char*user,char*pass)
{
charusername[MAXSIZE];
charpassword[MAXSIZE];
char*pch;
charbuf[MAXSIZE];
char*line=NULL;
size_tnum_read;
size_tlen=0;
FILE*fd;
intauth=0;
fd=fopen(".auth","r");
if(fd==NULL){
perror("filenotfound");
exit
(1);
}
while((num_read=getline(&line,&len,fd))!
=-1){
memset(buf,0,MAXSIZE);
strcpy(buf,line);
pch=strtok(buf,"");
strcpy(username,pch);
if(pch!
=NULL){
pch=strtok(NULL,"");
strcpy(password,pch);
}
//removeendoflineandwhitespace
trimstr(password,(int)strlen(password));
if((strcmp(user,username)==0)&&(strcmp(pass,password)==0)){
auth=1;
break;
}
}
free(line);
fclose(fd);
returnauth;
}
/**
*Loginconnectedclient
*/
intftserve_login(intsock_control)
{
charbuf[MAXSIZE];
charuser[MAXSIZE];
charpass[MAXSIZE];
memset(user,0,MAXSIZE);
memset(pass,0,MAXSIZE);
memset(buf,0,MAXSIZE);
//Waittorecieveusername
if((recv_data(sock_control,buf,sizeof(buf)))==-1){
perror("recverror\n");
exit
(1);
}
inti=5;
intn=0;
while(buf[i]!
=0)
user[n++]=buf[i++];
//tellclientwe'rereadyforpassword
send_response(sock_control,331);
//Waittorecievepassword
memset(buf,0,MAXSIZE);
if((recv_data(sock_control,buf,sizeof(buf)))==-1){
perror("recverror\n");
exit
(1);
}
i=5;
n=0;
while(buf[i]!
=0){
pass[n++]=buf[i++];
}
return(ftserve_check_user(user,pass));
}
/**
*Waitforcommandfromclientand
*sendresponse
*Returnsresponsecode
*/
intftserve_recv_cmd(intsock_control,char*cmd,char*arg)
{
intrc=200;
charbuffer[MAXSIZE];
memset(buffer,0,MAXSIZE);
memset(cmd,0,5);
memset(arg,0,MAXSIZE);
//Waittorecievecommand
if((recv_data(sock_control,buffer,sizeof(buffer)))==-1){
perror("recverror\n");
return-1;
}
strncpy(cmd,buffer,4);
char*tmp=buffer+5;
strcpy(arg,tmp);
if(strcmp(cmd,"QUIT")==0){
rc=221;
}elseif((strcmp(cmd,"USER")==0)||(strcmp(cmd,"PASS")==0)||
(strcmp(cmd,"LIST")==0)||(strcmp(cmd,"RETR")==0)){
rc=200;
}else{//invalidcommand
rc=502;
}
send_response(sock_control,rc);
returnrc;
}
/**
*Childprocesshandlesconnectiontoclient
*/
voidftserve_process(intsock_control)
{
intsock_data;
charcmd[5];
chararg[MAXSIZE];
//Sendwelcomemessage
send_response(sock_control,220);
//Authenticateuser
if(ftserve_login(sock_control)==1){
send_response(sock_control,230);
}else{
send_response(sock_control,430);
exit(0);
}
while
(1){
//Waitforcommand
intrc=ftserve_recv_cmd(sock_control,cmd,arg);
if((rc<0)||(rc==221)){
break;
}
if(rc==200){
//Opendataconnectionwithclient
if((sock_data=ftserve_start_data_conn(sock_control))<0){
close(sock_control);
exit
(1);
}
//Executecommand
if(strcmp(cmd,"LIST")==0){//Dolist
ftserve_list(sock_data,sock_control);
}elseif(strcmp(cmd,"RETR")==0){//Doget
ftserve_retr(sock_control,sock_data,arg);
}
//Closedataconnection
close(sock_data);
}
}
}
七、设计总结
通过本次的linux程序与设计课程设计让我们学会使用linux系统和socket,极大的丰富了linux系统下编程和网络方面的知识,锻炼了动手能力,为以后的工作学习打下了坚实的基础。