安徽工业大学计算机科学与技术学院
《Linux网络编程》
课程设计
班级:
姓名:
指导老师:
一、设计背景
?Linux?操作系统作为一个开源的操作系统被越来越多的人所应用,它的好处在于操作系统源代码的公开化!只要是基于GNU公约的软件你都可以任意使用并修改它的源代码。通过这次课程设计能更好的学习网络编程知识和掌握LINUX平台上应用程序设计开发的过程,将大学四年所学知识综合运用,为未来的工作学习打下基础。
二、设计目的
1、学习epoll 跟FTP被动模式
2、掌握linux基本命令 ,例如ls、cd、login;
3、学会如何编译、运行
三、环境要求
1、centos 64位操作系统
2、gcc编译器
四、设计原理
4.1客户端
客户端程序的主要任务有以下3个:
(1)、分析用户输入的命令。
(2)、根据命令向服务器发出请求
(3)、接受服务器返回请求的结果
客户端为用户提供了3种命令: (1)、get:从服务器下载文件 (2)、list:列出客户端当前目录的内容(3)、quit离开
4.2 服务器端
(1)、分析请求代码。
(2)、根据请求代码做相应的处理
(3)、等待返回结果或者应答信息
五、软件测试结果
六、部分主代码
#include "ftserve.h"
int main(int argc, char *argv[])
{
int sock_listen, sock_control, port, pid;
if (argc != 2) {
printf("usage: ./ftserve port\n");
exit(0);
}
port = atoi(argv[1]);
// create socket
if ((sock_listen = socket_create(port)) < 0 ) {
perror("Error creating socket");
exit(1);
}
while(1) {// wait for client request
// create new socket for control connection
if ((sock_control = socket_accept(sock_listen))< 0 )
break;
// create child process to do actual file transfer
if ((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, sending
* control message over control connection
* Handles case of null or invalid filename
*/
void ftserve_retr(int sock_control, int sock_data, char* filename)
{
FILE* fd = NULL;
char data[MAXSIZE];
size_t num_read;
fd = fopen(filename, "r");
if (!fd) {
// send error code (550 Requested action not taken)
send_response(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 block
if (send(s