TCP 기반 C/S 모델 파일 전송

76319 단어 배우다
구현: 파일 업로드 파일 다운로드 명령행을 통해 서버의 각 층 파일을 보기: cd, ls, mkdir, exit
server.c
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "struct.h"
#include 
#include 
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
"
,ret); for(struct dirent* de = readdir(dp);NULL != de;de =readdir(dp)) { strcat(buf,de->d_name); strcat(buf,"
"
); } 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
",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
",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
"
,ret); ret = write(fd,&buf,strlen(buf)); printf("write:%d
"
,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
",ret);
while(ret = read(fd,&buf,1000)) { printf("%d
"
,write(clifd,&buf,ret)); } } int main() { printf(" socket...
"
); int sockfd = socket(AF_INET,SOCK_STREAM,0); if(0 > sockfd) { perror("socket"); return -1; } printf(" ...
"
); 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 ...
"
); if(bind(sockfd,(struct sockaddr*)&addr,len)) { perror("bind"); return -1; } printf(" ...
"
); if(listen(sockfd,5)) { perror("listen"); return -1; } printf(" ...
"
); 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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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("--------------------------
"
); } } 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
",sizeof(serverbuf));
//memcpy(buf,&serverbuf,sizeof(sendto_server)); //printf("%d
",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
"
,ret); if (i == num) { ret = send(sockfd,&buf,strlen(buf)+1,0); break; } ret = send(sockfd,&buf,strlen(buf),0); // printf("%d
",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
"
,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
"
,serverbuf.num); for(int i=0;i<=serverbuf.num;i++) { ret =read(sockfd,&buf,1000); printf("%d
"
,ret); if (0 == strlen(buf)) { break; } write(fd,&buf,strlen(buf)); } close(fd); } int main() { printf(" socket...
"
); sockfd = socket(AF_INET,SOCK_STREAM,0); if(0 > sockfd) { perror("socket"); return -1; } printf(" ...
"
); 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(" ...
"
); if(connect(sockfd,(struct sockaddr*)&addr,len)) { perror("bind"); return -1; } printf("1、
"
); printf("2、
"
); printf("3、
"
); 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

좋은 웹페이지 즐겨찾기