基于TCP的C/S模型文件传输

实现:
文件上传
文件下载
通过命令行查看服务器的各层文件:cd、ls、mkdir、exit

server.c

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include "struct.h"
#include <pthread.h>
#include <dirent.h>
void* command_file(void* arg)
{
	int clifd = *(int*)arg;
	char buf[1024]={};
	char path[1024]={};
	strcpy(path,".");
	for(;;)
	{
		read(clifd,&buf,sizeof(buf));
		puts(buf);
		if (0 == strcmp("exit",buf))
		{
			break;
			write(clifd,&buf,strlen(buf)+1);
		}
		else if (0 == strcmp("ls",buf))
		{
			DIR *dp = opendir(path);
			int ret = chdir(path);
			printf("%d\n",ret);
			for(struct dirent* de = readdir(dp);NULL != de;de =readdir(dp))
			{
				strcat(buf,de->d_name);
				strcat(buf,"\n");
			}
			write(clifd,&buf,strlen(buf)+1);
		}
		else if ('c' == buf[0] && 'd' == buf[1])
		{
			//sprintf(path,"%s/%s",path,buf+3);
			puts(path);
			strcpy(path,buf+3);
			sprintf(buf,"进入:%s",path);
			write(clifd,&buf,strlen(buf)+1);
		}
		else if ('m' == buf[0] && 'k' == buf[1] && 'd' == buf[2] && 'i' ==buf[3]&& 'r' == buf[4])
		{
			char name[20]={};
			strcpy(name,buf+6);
			int ret = mkdir(name,0777);
			if (0 == ret)
			{
				strcpy(buf,"创建目录成功");
			}
			else
			{
				strcpy(buf,"创建目录失败");
			}
			write(clifd,&buf,strlen(buf)+1);
		}
		else
		{
			strcpy(buf,"无此命令");
			write(clifd,&buf,strlen(buf)+1);
		}
	}
}	
	

void* upload_file(void* arg)
{
	int clifd = *(int*)arg;
	char buf[1024] = {};
	struct stat fdbuf;
	struct sendto_server serverbuf;
	int ret = read(clifd,&serverbuf,sizeof(serverbuf));
	//printf("%d %d\n",ret,serverbuf.atime);
	int fd = open(serverbuf.name,O_RDONLY);
	if (0 >fd)
	{
		strcpy(buf,"error");
		fd = open(serverbuf.name,O_CREAT|O_EXCL|O_WRONLY,0644);
	}
	else 
	{
		strcpy(buf,"yes");
	}
	ret = write(clifd,&buf,strlen(buf)+1);
	//printf("%d\n",ret);
	if (0 == strcmp("error",buf))
	{
		for(int i=0;i<=serverbuf.num;i++)
		{
			ret = read(clifd,&buf,1000);
			if (0 == strlen(buf))
			{
				break;
			}
			printf("read:%d\n",ret);
			ret = write(fd,&buf,strlen(buf));
			printf("write:%d\n",ret);
		}
		close(fd);
	}
}

void* download_file(void* arg)
{
	struct sendto_server serverbuf;
	int clifd = *(int*)arg;
	char path[1024]={};
	char buf[1024]={};
	struct stat fdbuf;
	read(clifd,&path,sizeof(path));
	puts(path);
	int fd = open(path,O_RDONLY);
	if (0 >fd)
	{
		strcpy(buf,"文件不存在");
		write(clifd,&buf,strlen(buf)+1);	
		return NULL;
	}
	else
	{
		strcpy(buf,"文件已打开,准备传输");
		write(clifd,&buf,strlen(buf)+1);
	}
	usleep(100);	
	fstat(fd,&fdbuf);
	int num =(fdbuf.st_size/1000);
	serverbuf.num = num;
	int ret=0;
	write(clifd,&serverbuf,sizeof(sendto_server));
	//printf("serverbuf:%d\n",ret);
	while(ret = read(fd,&buf,1000))
	{
		printf("%d\n",write(clifd,&buf,ret));
	}
}



int main()
{
	printf("服务器创建socket...\n");
	int sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(0 > sockfd)
	{
		perror("socket");
		return -1;
	}

	printf("准备地址...\n");
	struct sockaddr_in addr = {};
	addr.sin_family = AF_INET;
	addr.sin_port = htons(7777);
	addr.sin_addr.s_addr = inet_addr("192.168.43.183");
	socklen_t len = sizeof(addr);

	printf("绑定socket与地址...\n");
	if(bind(sockfd,(struct sockaddr*)&addr,len))
	{
		perror("bind");
		return -1;
	}

	printf("设置监听...\n");
	if(listen(sockfd,5))
	{
		perror("listen");
		return -1;
	}

	printf("等待客户端连接...\n");
	for(;;)
	{
		struct sockaddr_in addrcli = {};
		int clifd = accept(sockfd,(struct sockaddr*)&addrcli,&len);
		if(0 > clifd)
		{
			perror("accept");
			continue;
		}
		
		char buf[1024]={};
		void *str;
		read(clifd,&buf,sizeof(buf));
		if (0 == strcmp("1",buf))
		{
			pthread_t pid1;
			pthread_create(&pid1,NULL,command_file,&clifd);
			pthread_join(pid1,&str);
		}
		if (0 == strcmp("2",buf))
		{
			pthread_t pid2;
			pthread_create(&pid2,NULL,upload_file,&clifd);
			pthread_join(pid2,&str);
		}
		if (0 == strcmp("3",buf))
		{
			pthread_t pid3;
			pthread_create(&pid3,NULL,download_file,&clifd);
			pthread_join(pid3,&str);
		}
	}
}

client.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "struct.h"

struct sendto_server serverbuf;
int sockfd;

void command_file()
{
	stdin->_IO_read_ptr=stdin->_IO_read_end;
	char buf[1024]={};
	for(;;)
	{
		printf("请输入您要输入的命令");
		gets(buf);
		send(sockfd,&buf,strlen(buf)+1,0);
		if (0 == strcmp("exit",buf))
		{
			break;
		}
		read(sockfd,&buf,sizeof(buf));
		puts(buf);
		printf("--------------------------\n");
	}
}


void upload_file()
{
	stdin->_IO_read_ptr=stdin->_IO_read_end;
	char buf[1024]={};
	char path[255]={};
	char name[20]={};
	struct stat fdbuf;
	printf("请输入要发送的文件的路径");
	gets(path);
	printf("请输入要发送的文件的名称");
	gets(name);
	sprintf(path,"%s/%s",path,name);
	int fd = open(path,O_RDONLY);
	if (0 > fd)
	{
		perror("open");
		return ;
	}
	fstat(fd,&fdbuf);
	int num = (fdbuf.st_size/1000);
	strcpy(serverbuf.name,name);
	serverbuf.atime = fdbuf.st_atime;
	serverbuf.mtime = fdbuf.st_mtime;
	serverbuf.ctime = fdbuf.st_ctime;
	serverbuf.size = fdbuf.st_size;
	serverbuf.num = num;
	//memset(buf,0,4096);
	//printf("%d\n",sizeof(serverbuf));
	//memcpy(buf,&serverbuf,sizeof(sendto_server));
	//printf("%d\n",serverbuf.atime);	
	
	int len_write =send(sockfd,&serverbuf,sizeof(sendto_server),0);
	read(sockfd,&buf,sizeof(buf));
	puts(buf);
	if (0 == strcmp("error",buf))
	{	
		for(int i=0;i<=num;i++)
		{
			char buf[1024]={};
			int ret = read(fd,&buf,1000);
			printf("%d\n",ret);
			if (i == num)
			{
				ret = send(sockfd,&buf,strlen(buf)+1,0);
				break;
			}
			ret = send(sockfd,&buf,strlen(buf),0);
		//	printf("%d\n",ret);
		}
	}
}

void download_file()
{
	stdin->_IO_read_ptr=stdin->_IO_read_end;
	char path[1024]={};
	char name[1024]={};
	char buf[1024]={};
	printf("请输入要下载的文件的服务器路径");
	gets(path);
	printf("请输入要下载的文件的名称");
	gets(name);
	sprintf(path,"%s/%s",path,name);
	write(sockfd,&path,strlen(path)+1);
	printf("%d\n",read(sockfd,&buf,sizeof(buf)));
	puts(buf);
	if (0 == strcmp(buf,"文件不存在"))
	{
		return;
	}
	int fd = open(name,O_CREAT|O_EXCL|O_WRONLY,0644);
	if (0 > fd)
	{
		printf("文件已存在");
	}
	int ret=0;
	read(sockfd,&serverbuf,sizeof(sendto_server));
	//printf("serverbuf:%d",ret);
	printf("%d\n",serverbuf.num);
	for(int i=0;i<=serverbuf.num;i++)
	{
		ret =read(sockfd,&buf,1000);
		printf("%d\n",ret);
		if (0 == strlen(buf))
		{
			break;
		}
		write(fd,&buf,strlen(buf));
	}
	close(fd);
}

int main()
{
	
	printf("服务器创建socket...\n");
	sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(0 > sockfd)
	{
		perror("socket");
		return -1;
	}

	printf("准备地址...\n");
	struct sockaddr_in addr = {};
	addr.sin_family = AF_INET;
	addr.sin_port = htons(7777);
	addr.sin_addr.s_addr = inet_addr("192.168.43.183");
	socklen_t len = sizeof(addr);

	printf("绑定连接服务器...\n");
	if(connect(sockfd,(struct sockaddr*)&addr,len))
	{
		perror("bind");
		return -1;
	}
	printf("1、输入命令\n");
	printf("2、上传文件\n");
	printf("3、下载文件\n");
	int n=0;
	scanf("%d",&n);
	char buf[1024]={};
	if (1==n) 
	{
		strcpy(buf,"1");
		send(sockfd,&buf,strlen(buf)+1,0);
		command_file();
	}
	if (2==n) 
	{
		strcpy(buf,"2");
		send(sockfd,&buf,strlen(buf)+1,0);
		upload_file();
	}
	if (3==n) 
	{
		strcpy(buf,"3");
		send(sockfd,&buf,strlen(buf)+1,0);
		download_file();
	}
	
	close(sockfd);
}

struct.h

#ifndef STRUCT_H
#define STRUCT_H

typedef struct sendto_server
{
	//char buf[1024];
	char name[20];
	int  size;
	int  atime;
	int  mtime;
	int  ctime;
	int  num;
}sendto_server;





#endif//STRUCT_H
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值