linux网络编程课程设计Word文档下载推荐.docx
- 文档编号:13514403
- 上传时间:2022-10-11
- 格式:DOCX
- 页数:14
- 大小:368.02KB
linux网络编程课程设计Word文档下载推荐.docx
《linux网络编程课程设计Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《linux网络编程课程设计Word文档下载推荐.docx(14页珍藏版)》请在冰豆网上搜索。
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)
{
FILE*fd;
intrs=system("
ls-l|tail-n+2>
tmp.txt"
if(rs<
fd=fopen("
tmp.txt"
"
fd){
/*Seektothebeginningofthefile*/
fseek(fd,SEEK_SET,0);
send_response(sock_control,1);
//starting
memset(data,0,MAXSIZE);
while((num_read=fread(data,1,MAXSIZE,fd))>
if(send(sock_data,data,num_read,0)<
err"
memset(data,0,MAXSIZE);
fclose(fd);
send_response(sock_control,226);
//send226
*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)<
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))<
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_tlen=0;
intauth=0;
.auth"
if(fd==NULL){
filenotfound"
}
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;
}
free(line);
returnauth;
/**
*Loginconnectedclient
intftserve_login(intsock_control)
charuser[MAXSIZE];
charpass[MAXSIZE];
memset(user,0,MAXSIZE);
memset(pass,0,MAXSIZE);
memset(buf,0,MAXSIZE);
//Waittorecieveusername
if
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- linux 网络 编程 课程设计
![提示](https://static.bdocx.com/images/bang_tan.gif)