FTP项目:实现客户端的连接(上传,下载等操作)

  • 服务端
#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>

#define LS 0
#define PWD 1
#define CD 2
#define GET 3
#define PUT 4
#define QUIT 5
#define U_LS 6
#define U_CD 7
#define U_PWD 8
#define DOFILE 9
#define RM 10
struct Msg
{
   
   
	int num;
	char data[1024];
	char pebuf[1024];
};

char *get_set(char *msg)
{
   
   
	char *a;
	a = strtok(msg," ");//分割字符串
	a = strtok(NULL," ");
	return a;
}

int get_client_num(char *data)
{
   
   
	if(strcmp("ls",data) == 0)  return LS;//等于为0,小于为负
	if(strcmp("pwd",data) == 0)  return PWD;
	if(strstr(data,"cd") != NULL)  return CD;
	if(strstr(data,"get") != NULL)  return GET;
	if(strstr(data,"put") != NULL)  return PUT;
	if(strcmp("quit",data) == 0)  return QUIT;
	if(strstr(data,"rm") != NULL)  return RM;
	//if(strcmp("uls",data) == 0)  return U_LS;
	//if(strcmp("upwd",data) == 0)  return U_PWD;
	//if(strstr(data,"ucd") != NULL)  return U_CD;
	return 666;
}

void msg_handle(struct Msg msg,int fd)
{
   
   
	char file_buf[1024] = {
   
   0};
	FILE *z = NULL;
	printf("client request: %s\n",msg.data);
	int ret = get_client_num(msg.data);
	char *set = NULL;
	int a;
	char *request = NULL;
	switch(ret)
	{
   
   
		case LS:
		case PWD:
			msg.num = 0;
			z = popen(msg.data,"r");
			a = fread(msg.data,sizeof(msg.data),1,z);
			if(a<0){
   
   printf("error\n"); perror("fread");}
			write(fd,&msg,sizeof(msg));
			break;
			
		case CD:
			msg.num = 1;
			set = get_set(msg.data);
			
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

llechee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值