linux网络编程课程设计(8页).doc

上传人:1595****071 文档编号:35420182 上传时间:2022-08-21 格式:DOC 页数:8 大小:151KB
返回 下载 相关 举报
linux网络编程课程设计(8页).doc_第1页
第1页 / 共8页
linux网络编程课程设计(8页).doc_第2页
第2页 / 共8页
点击查看更多>>
资源描述

《linux网络编程课程设计(8页).doc》由会员分享,可在线阅读,更多相关《linux网络编程课程设计(8页).doc(8页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、-linux网络编程课程设计-第 8 页Linux网络编程课程设计班级:姓名:指导老师:一、设计背景Linux操作系统作为一个开源的操作系统被越来越多的人所应用,它的好处在于操作系统源代码的公开化!只要是基于GNU公约的软件你都可以任意使用并修改它的源代码。通过这次课程设计能更好的学习网络编程知识和掌握LINUX平台上应用程序设计开发的过程,将大学四年所学知识综合运用,为未来的工作学习打下基础。二、设计目的1、学习epoll 跟FTP被动模式2、掌握linux基本命令 ,例如ls、cd、login;3、学会如何编译、运行三、环境要求1、centos 64位操作系统2、gcc编译器四、设计原理4

2、1客户端 客户端程序的主要任务有以下3个: (1)、分析用户输入的命令。 (2)、根据命令向服务器发出请求 (3)、接受服务器返回请求的结果 客户端为用户提供了3种命令: (1)、get:从服务器下载文件 (2)、list:列出客户端当前目录的内容(3)、quit离开4.2 服务器端(1)、分析请求代码。 (2)、根据请求代码做相应的处理 (3)、等待返回结果或者应答信息五、软件测试结果六、部分主代码#include ftserve.hint main(int argc, char *argv)int sock_listen, sock_control, port, pid;if (argc

3、!= 2) printf(usage: ./ftserve portn);exit(0);port = atoi(argv1);/ create socketif (sock_listen = socket_create(port) 0 ) perror(Error creating socket);exit(1);while(1) / wait for client request/ create new socket for control connectionif (sock_control = socket_accept(sock_listen) 0 )break;/ create c

4、hild process to do actual file transferif (pid = fork() 0) perror(Error forking child process); else if (pid = 0) close(sock_listen);ftserve_process(sock_control);close(sock_control);exit(0);close(sock_control);close(sock_listen);return 0; * Send file specified in filename over data connection, send

5、ing * control message over control connection * Handles case of null or invalid filenamevoid ftserve_retr(int sock_control, int sock_data, char* filename)FILE* fd = NULL;char dataMAXSIZE;size_t num_read;fd = fopen(filename, r);if (!fd) / send error code (550 Requested action not taken)send_response(

6、sock_control, 550); else / send okay (150 File status okay)send_response(sock_control, 150);do num_read = fread(data, 1, MAXSIZE, fd);if (num_read 0) printf(error in fread()n);/ send blockif (send(sock_data, data, num_read, 0) 0);/ send message: 226: closing conn, file transfer successfulsend_respon

7、se(sock_control, 226);fclose(fd); * Send list of files in current directory * over data connection * Return -1 on error, 0 on successint ftserve_list(int sock_data, int sock_control)char dataMAXSIZE;size_t num_read;FILE* fd;int rs = system(ls -l | tail -n+2 tmp.txt);if ( rs 0) if (send(sock_data, da

8、ta, num_read, 0) 0) perror(err);memset(data, 0, MAXSIZE);fclose(fd);send_response(sock_control, 226);/ send 226return 0; * Open data connection to client * Returns: socket for data connection * or -1 on errorint ftserve_start_data_conn(int sock_control)char buf1024;int wait, sock_data;/ Wait for go-

9、ahead on control connif (recv(sock_control, &wait, sizeof wait, 0) 0 ) perror(Error while waiting);return -1;/ Get client addressstruct sockaddr_in client_addr;socklen_t len = sizeof client_addr;getpeername(sock_control, (struct sockaddr*)&client_addr, &len);inet_ntop(AF_INET, &client_addr.sin_addr,

10、 buf, sizeof(buf);/ Initiate data connection with clientif (sock_data = socket_connect(CLIENT_PORT_ID, buf) 0)return -1;return sock_data; * Authenticate a users credentials * Return 1 if authenticated, 0 if notint ftserve_check_user(char*user, char*pass)char usernameMAXSIZE;char passwordMAXSIZE;char

11、 *pch;char bufMAXSIZE;char *line = NULL;size_t num_read;size_t len = 0;FILE* fd;int auth = 0;fd = fopen(.auth, r);if (fd = NULL) perror(file not found);exit(1);while (num_read = getline(&line, &len, fd) != -1) memset(buf, 0, MAXSIZE);strcpy(buf, line);pch = strtok (buf, );strcpy(username, pch);if (p

12、ch != NULL) pch = strtok (NULL, );strcpy(password, pch);/ remove end of line and whitespacetrimstr(password, (int)strlen(password);if (strcmp(user,username)=0) & (strcmp(pass,password)=0) auth = 1;break;free(line);fclose(fd);return auth; * Log in connected clientint ftserve_login(int sock_control)ch

13、ar bufMAXSIZE;char userMAXSIZE;char passMAXSIZE;memset(user, 0, MAXSIZE);memset(pass, 0, MAXSIZE);memset(buf, 0, MAXSIZE);/ Wait to recieve usernameif ( (recv_data(sock_control, buf, sizeof(buf) ) = -1) perror(recv errorn); exit(1);int i = 5;int n = 0;while (bufi != 0)usern+ = bufi+;/ tell client we

14、re ready for passwordsend_response(sock_control, 331);/ Wait to recieve passwordmemset(buf, 0, MAXSIZE);if ( (recv_data(sock_control, buf, sizeof(buf) ) = -1) perror(recv errorn); exit(1);i = 5;n = 0;while (bufi != 0) passn+ = bufi+;return (ftserve_check_user(user, pass); * Wait for command from cli

15、ent and * send response * Returns response codeint ftserve_recv_cmd(int sock_control, char*cmd, char*arg)int rc = 200;char bufferMAXSIZE;memset(buffer, 0, MAXSIZE);memset(cmd, 0, 5);memset(arg, 0, MAXSIZE);/ Wait to recieve commandif (recv_data(sock_control, buffer, sizeof(buffer) ) = -1) perror(rec

16、v errorn); return -1;strncpy(cmd, buffer, 4);char *tmp = buffer + 5;strcpy(arg, tmp);if (strcmp(cmd, QUIT)=0) rc = 221; else if(strcmp(cmd, USER)=0) | (strcmp(cmd, PASS)=0) |(strcmp(cmd, LIST)=0) | (strcmp(cmd, RETR)=0) rc = 200; else /invalid commandrc = 502;send_response(sock_control, rc);return r

17、c; * Child process handles connection to clientvoid ftserve_process(int sock_control)int sock_data;char cmd5;char argMAXSIZE;/ Send welcome messagesend_response(sock_control, 220);/ Authenticate userif (ftserve_login(sock_control) = 1) send_response(sock_control, 230); else send_response(sock_contro

18、l, 430);exit(0);while (1) / Wait for commandint rc = ftserve_recv_cmd(sock_control, cmd, arg);if (rc 0) | (rc = 221) break;if (rc = 200 ) / Open data connection with clientif (sock_data = ftserve_start_data_conn(sock_control) 0) close(sock_control);exit(1); / Execute commandif (strcmp(cmd, LIST)=0) / Do listftserve_list(sock_data, sock_control); else if (strcmp(cmd, RETR)=0) / Do get ftserve_retr(sock_control, sock_data, arg);/ Close data connectionclose(sock_data);七、设计总结通过本次的linux程序与设计课程设计让我们学会使用linux系统和socket,极大的丰富了linux系统下编程和网络方面的知识,锻炼了动手能力,为以后的工作学习打下了坚实的基础。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 单元课程

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁