当前位置:文档之家› linux网络编程课程设计

linux网络编程课程设计

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 离开
服务器端
(1)、分析请求代码。

(2)、根据请求代码做相应的处理
(3)、等待返回结果或者应答信息
五、软件测试结果
六、部分主代码
#include "" 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]);
uth", "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 (pch != NULL) { pch = strtok (NULL, " "); strcpy(password, pch);
}
// remove end of line and whitespace trimstr(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 client
*/
int ftserve_login(int sock_control)
{
char buf[MAXSIZE];
char user[MAXSIZE];
char pass[MAXSIZE]; memset(user, 0, MAXSIZE); memset(pass, 0, MAXSIZE);
memset(buf, 0, MAXSIZE);
// Wait to recieve username
if ( (recv_data(sock_control, buf, sizeof(buf)) ) == -1) { perror("recv error\n");
exit(1);
}
int i = 5;
int n = 0; while (buf[i] != 0) user[n++] = buf[i++];
// tell client we're ready for password send_response(sock_control, 331);
// Wait to recieve password
memset(buf, 0, MAXSIZE);
if ( (recv_data(sock_control, buf, sizeof(buf)) ) == -1) { perror("recv error\n"); exit(1);
}
i = 5;
n = 0;
while (buf[i] != 0) {
pass[n++] = buf[i++]; }
return (ftserve_check_user(user, pass)); }
* Wait for command from client and
* send response
* Returns response code
*/
int ftserve_recv_cmd(int sock_control, char*cmd, char*arg) {
int rc = 200;
char buffer[MAXSIZE];
memset(buffer, 0, MAXSIZE); memset(cmd, 0, 5); memset(arg, 0, MAXSIZE);
// Wait to recieve command
if ((recv_data(sock_control, buffer, sizeof(buffer)) ) == -1) { perror("recv error\n");
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 command rc = 502;
}
send_response(sock_control, rc); return rc;
}
* Child process handles connection to client
*/
void ftserve_process(int sock_control)
{
int sock_data;
char cmd[5];
char arg[MAXSIZE];
// Send welcome message send_response(sock_control, 220);
// Authenticate user
if (ftserve_login(sock_control) == 1) { send_response(sock_control, 230);
} else { send_response(sock_control, 430); exit(0);
}
while (1) {
// Wait for command
int rc = ftserve_recv_cmd(sock_control, cmd, arg);
if ((rc < 0) || (rc == 221)) { break;
}
if (rc == 200 ) {
// Open data connection with client
if ((sock_data = ftserve_start_data_conn(sock_control)) < 0)
{ close(sock_control);
exit(1);
}
// Execute command
if (strcmp(cmd, "LIST")==0) { // Do list ftserve_list(sock_data, sock_control);
} else if (strcmp(cmd, "RETR")==0) { // Do get
<filename> ftserve_retr(sock_control, sock_data, arg);
}
// Close data connection
close(sock_data);
}
}
}
七、设计总结
通过本次的linux 程序与设计课程设计让我们学会使用linux 系统和socket ,极大的丰富了linux 系统下编程和网络方面的知识,锻炼了动手能力,为以后的工作学习打下了坚实的基础。

相关主题