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 离开

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))

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))

break;

// create child process to do actual file transfer

if ((pid = fork())

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

printf("error in fread()\n");

}

// send block

if (send(sock_data, data, num_read, 0)

perror("error sending file\n");

} while (num_read > 0);

// send message: 226: closing conn, file transfer successful send_response(sock_control, 226);

fclose(fd);

}

}

/**

* Send list of files in current directory

* over data connection

* Return -1 on error, 0 on success

*/

int ftserve_list(int sock_data, int sock_control)

{

char data[MAXSIZE];

size_t num_read;

FILE* fd;

int rs = system("ls -l | tail -n+2 > tmp.txt");

if ( rs

exit(1);

}

fd = fopen("tmp.txt", "r");

if (!fd) {

exit(1);

}

/* Seek to the beginning of the file */

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)

perror("err");

}

memset(data, 0, MAXSIZE);

}

fclose(fd);

send_response(sock_control, 226); // send 226

return 0;

}

/**

* Open data connection to client

* Returns: socket for data connection

* or -1 on error

*/

int ftserve_start_data_conn(int sock_control)

{

char buf[1024];

int wait, sock_data;

// Wait for go-ahead on control conn

if (recv(sock_control, &wait, sizeof wait, 0)

perror("Error while waiting");

return -1;

}

// Get client address

struct 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, buf, sizeof(buf));

// Initiate data connection with client

if ((sock_data = socket_connect(CLIENT_PORT_ID, buf))

return -1;

return sock_data;

}

/**

* Authenticate a user's credentials

* Return 1 if authenticated, 0 if not

*/

int ftserve_check_user(char*user, char*pass)

{

char username[MAXSIZE];

char password[MAXSIZE];

char *pch;

char buf[MAXSIZE];

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 (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 ftserve_retr(sock_control, sock_data, arg); } // Close data connection close(sock_data);

七、设计总结

通过本次的linux 程序与设计课程设计让我们学会使用linux 系统和socket ,极大的丰富了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 离开

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))

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))

break;

// create child process to do actual file transfer

if ((pid = fork())

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

printf("error in fread()\n");

}

// send block

if (send(sock_data, data, num_read, 0)

perror("error sending file\n");

} while (num_read > 0);

// send message: 226: closing conn, file transfer successful send_response(sock_control, 226);

fclose(fd);

}

}

/**

* Send list of files in current directory

* over data connection

* Return -1 on error, 0 on success

*/

int ftserve_list(int sock_data, int sock_control)

{

char data[MAXSIZE];

size_t num_read;

FILE* fd;

int rs = system("ls -l | tail -n+2 > tmp.txt");

if ( rs

exit(1);

}

fd = fopen("tmp.txt", "r");

if (!fd) {

exit(1);

}

/* Seek to the beginning of the file */

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)

perror("err");

}

memset(data, 0, MAXSIZE);

}

fclose(fd);

send_response(sock_control, 226); // send 226

return 0;

}

/**

* Open data connection to client

* Returns: socket for data connection

* or -1 on error

*/

int ftserve_start_data_conn(int sock_control)

{

char buf[1024];

int wait, sock_data;

// Wait for go-ahead on control conn

if (recv(sock_control, &wait, sizeof wait, 0)

perror("Error while waiting");

return -1;

}

// Get client address

struct 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, buf, sizeof(buf));

// Initiate data connection with client

if ((sock_data = socket_connect(CLIENT_PORT_ID, buf))

return -1;

return sock_data;

}

/**

* Authenticate a user's credentials

* Return 1 if authenticated, 0 if not

*/

int ftserve_check_user(char*user, char*pass)

{

char username[MAXSIZE];

char password[MAXSIZE];

char *pch;

char buf[MAXSIZE];

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 (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 ftserve_retr(sock_control, sock_data, arg); } // Close data connection close(sock_data);

七、设计总结

通过本次的linux 程序与设计课程设计让我们学会使用linux 系统和socket ,极大的丰富了linux 系统下编程和网络方面的知识,锻炼了动手能力,为以后的工作学习打下了坚实的基础。


相关内容

  • 我的大学计算机生活
  • 我是2005届的学生,毕业快一年了,对自己的大学生活做简要的回顾,并说明一下自己认为的学习计算机比较重要的课程,并推荐一些好书. 我从大一下学期开始学习计算机,刚开始学习VB(学校的公选课) ,认真学习了两个月之后,发现学习VB 主要是拖放一些按钮控件,没有一点挑战性,学会后,也不能构成自己的核心竞 ...

  • [局域网组建与维护实训]课程设计指导书 (3)
  • 局域网组建与维护实训 课程设计说明书 江西工程学院 2017年4月 目 录 一.设计任务书 二.设计内容 (一)制定校园网设计方案 (二)组建.维护小型局域网 1. 制作双绞线及安装网卡 2.LINUX 服务器部署安装 3. 网络综合布线 4. 组建无线局域网络 5. 维护网络(部分为选做内容) ( ...

  • 如何才能成为一名linux专家
  • linux专家本身这个概念就是一个笼统的概念.就好比说想成为一个医学专家.对于其他职业的人来说,这个称谓就够了.在医学内部则是一个笼统的概念.医学分为内科.外科.骨科.妇产科.儿科.神经科等等.妇产科专家当然干不了骨科专家的工作.linux领域也是同样的道理.但是正如自然辩证法所言:有共性与个性的区 ...

  • 嵌入式系统课程设计选题要求及题目
  • 嵌入式系统课程设计-选题要求及课题 1.嵌入式系统课程设计时长两星期,要求学生分组进行课程设计,每组学生人数为2-3人(可在不超过3人的范围内由指导教师具体规定),报告雷同超过60%者,成绩都记不及格! 2.学生需要在附后的设计题目总表中进行选题,原则上需要在6月17号前完成选题,并开始课程设计工作 ...

  • 大学生必学的软件综合
  • (一) 这里我推荐大家十个必学的计算机软件,希望大家在大学空余时间多多学习: 1.windows XP/Vista/7+ 系统 2.windows 优化大师.超级兔子.360 安全设置等 3.杀毒软件的安装和使用 4.IE.Firefox 等网页浏览器 5.Foxmail.Outlook 电子邮箱客 ...

  • 网络工程专业就业及考研
  • 本文由aaa99992贡献 pdf文档可能在WAP端浏览体验不佳.建议您优先选择TXT,或下载源文件到本机查看. <网络工程专业导论>课程 <网络工程专业导论>课程 (Introduction to Network Engineering) (Introduction to ...

  • 信息安全专业课程书籍推荐
  • [转载]武大信息安全专业课程 简介(四) (2011-07-10 01:16:12) 转载原文标签: 转载原文地址:武大信息安全专业课程简介(四)作者:柏拉图的思念 1模式识别Pattern Recognition7. 课程简介 本课程是信息安全专业的专业选修课.模式识别是一门理论与应用并重的技术科 ...

  • linux嵌入式学习路线(新版)
  • 嵌入式学习路线图 嵌入式开发学习路线图 为什么选择学习嵌入式? 嵌入式系统无疑是当前最热门最有发展前途的IT应用领域之一,同时也是当今IT领域仅存的几个金领职位之一.当前的中国IT人才面临严重的"后继乏人", 而且这种缺口由于培训缺乏.教育模式等原因造成的,而缺口最大的,就是高级 ...

  • 网络工程师
  • 无锡网络工程师培训 无锡网络工程师培训 课程介绍 ·最全面最专业的网络管理.编程.网络安全及网络设备工程师培养方案.近几年网络管理.编程人员及网络安全人员成为IT行业的紧缺人才,面对这样的就业情况,我门根据就业市场量身定制了这门课程,在整个就业课程中包含了5大知识体系,即Windows平台管理及维护 ...