socket网络编程实现的简易ftp项目

一、config.h

#define LLS     1
#define LS      2
#define LCD     3
#define CD      4
#define PUT     5
#define GET     6
#define QUIT    7
#define PWD     8

struct Msg
{
   
        char data[128];
        char Buf[1024];
};

二、服务器端demo:

#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "config.h"		/* 存放define宏定义和结构体 */

int cmd_Get(char *cmd)	/* 用于判断收到指令的类型 */
{
   
	if(strcmp("ls",cmd) == 0)       return LS;
	if(strcmp("pwd",cmd) == 0)       return PWD;
	if(strcmp("quit",cmd) == 0)       return QUIT;

	if(strstr(cmd,"cd") != NULL)    return CD;
	if(strstr(cmd,"get") != NULL)    return GET;
	if(strstr(cmd,"put") != NULL)    return PUT;

	return -1;
}

char *file_Get(char *msg)	/* 用与将指令后的文件名提取出来,并返回 */
{
   
	char *p;

	p = strtok(msg," ");
	p = strtok(NULL," ");

	return p;
}

void cmd_handler(struct Msg msg,int fd)		/* 用于处理接收到的指令 */
{
   
	int ret = cmd_Get(msg.data);
	int filefd;

	FILE *fp;

	char *dir = NULL;

	struct Msg readBuf;

	switch(ret){
   
		case LS:
		case PWD:
			fp = popen(msg.data,"r");
			fread(msg.Buf,sizeof(msg.Buf),1,fp);
			write(fd,&msg,sizeof(struct Msg));
			break;
		case CD:
			dir = file_Get(ms
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值