tcp实现简单的文件服务器(只能传文件)

程序实现如下功能:

1.ls  --  显示服务器下的文件列表

2.get filename  --  下载文件(服务器下载到客户端)

3.put filename  --  上传文件(客户端上传到服务器)

4.remove filename -- 删除文件(删除服务器的文件)

ps:自己写的代码太烂了,自己都不想看,跟看到一堆翔一样。。。。。大哭

服务器端:

/*************************************************************************
	> File Name: file_server.c
	> Author: qinf
	> Mail: 
	> Created Time: Sat 05 Apr 2014 09:40:25 AM CST
 ************************************************************************/

#include "file_server.h"

#define IN 1
#define OUT 0
#define MAX_LEN 128
//处理client发来的命令
void *thread_function(void *arg);
void cmd_handle(char *cmd, char *result);

int main(int argc, char *argv[])
{
	//变量定义
	int server_fd, client_fd, iret;
	pthread_t a_thread;
	struct sockaddr_in server_addr, client_addr;
	struct thread_msg message;
	memset(&message, '\0', sizeof(message));
	char result[128];
	char cmd[128];

	memset(cmd, '\0', 128);
	memset(result, '\0', 128);
	
	//socket
	server_fd = socket(AF_INET, SOCK_STREAM, 0);
	if (-1 == server_fd) {
		perror("socket");
		exit(EXIT_FAILURE);
	}
	//bind
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(atoi(argv[2]));
	server_addr.sin_addr.s_addr = inet_addr(argv[1]);
	 int on=1;  
    if((setsockopt(server_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)))<0)  
    {  
        perror("setsockopt failed");  
        exit(EXIT_FAILURE);  
    }  
	iret = bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
	if (-1 == iret) {
		perror("bind");
		exit(EXIT_FAILURE);;
	}
	//listen
	iret = listen(server_fd, 5);
	if (-1 == iret) {
		perror("listen");
		exit(EXIT_FAILURE);
	}
	//accept,交给线程处理
	int len= sizeof(client_addr);
	while (1) {
		client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &len);
		if (-1 == client_fd) {
			perror("client_fd");
			exit(EXIT_FAILURE);
		}
		iret = pthread_create(&a_thread, NULL, (void *)thread_function, &client_fd);
		if (0 != iret) {
			perror("pthread_create");
			exit(EXIT_FAILURE);
		}
	}
	//close
	close(server_fd);
}

//处理client发来的命令
void *thread_function(void *arg) {
	int client_fd = *(int *)arg;
	char cmd[128];
	char result[128];
	DIR *dir;
	struct dirent *dir_ptr;
	char *filenames[MAX_LEN];
	int num = 0, i = 0, iret;
	int fd;

	dir = opendir("./");
	if (NULL == dir) {
		perror("opendir");
		exit(EXIT_FAILURE);
	}
	while ((dir_ptr = readdir(dir)) != NULL) {
		filenames[num] = (char *)malloc(sizeof(char) * 128);
		memset(filenames[num], '\0', 128);
		strcpy(filenames[num], dir_ptr->d_name);
		printf("%s\n", filenames[num]);//打印目录
		++num;
		printf("num=%d\n", num);
	}
	while (1) {
		memset(cmd, '\0', 128);
		memset(result, '\0', 128);
		// recv(client_fd, cmd, 128, 0);
		recv(client_fd, cmd, 9, 0);
		printf("cmd===%s\n", cmd);
		strcpy(result, cmd);
		printf("%s\n", result);
		//处理命令
		i = 0;
		char name[128];
		char buf[128];
		int len;

		if (strncmp(result, "ls", 2) == 0) {
			send(client_fd, &num, sizeof(int), 0);
			while (filenames[i] != NULL) {
				filenames[i][strlen(filenames[i])] = '\n';
				send(client_fd, filenames[i], strlen(filenames[i]), 0);
				//printf("%s\n", filenames[i]);
				++i;
			}
		} else if(strncmp(result, "remove", 6) == 0) {
			//name = &result[7];
			sprintf(name, "./%s", &result[7]);
			name[strlen(name)-1] = '\0';
			iret = unlink(name);
			if (-1 == iret) {
				perror("unlink");
				exit(EXIT_FAILURE);
			} else {
				memset(result, '\0', 128);
				strcpy(result, "file removed!");
				iret = send(client_fd, &result, strlen(result), 0);
				if (-1 == iret) {
					perror("send");
					exit(EXIT_FAILURE);
				}
			}
		} else if (strncmp(result, "get", 3) == 0) {
			sprintf(name, "./%s", &result[4]);
			printf("name=%s,,len=%d\n", name, strlen(name));
			fd = open(name, O_RDONLY);
			if(-1 == fd) {
				perror("open");
				exit(EXIT_FAILURE);
			}
			memset(buf, '\0', 128);
			while ((iret = read(fd, buf, 128)) > 0) {
				send(client_fd, buf, iret, 0);
				memset(buf, '\0', 128);
			}
			close(fd);
			printf("translate over!\n");
		} else if (strncmp(result, "put", 3) == 0) {
			off_t file_size;
			off_t tmp = 0;
			memset(name, '\0', 128);
			memset(buf, '\0', 128);
			strcpy(name, &result[4]);
			printf("name=%s\n", name);

			fd = open(name, O_WRONLY|O_CREAT, 0666);
			if (-1 == fd) {
				perror("open file");
				exit(EXIT_FAILURE);
			}
			printf("fd=%d\n", fd);
			iret = recv(client_fd, &file_size, sizeof(file_size), 0);
			if (-1 == iret) {
				perror("recv");
				exit(EXIT_FAILURE);
			}
			while((iret = recv(client_fd, buf, 128, 0)) > 0) {
				write(fd, buf, iret);
				tmp += iret;
				if (file_size == tmp)
					break;
				memset(buf, '\0', 128);
			}
			printf("put over!\n");
		} else if (strncmp(result, "quit", 4) == 0) {
			printf("result=%s,%d\n", result,strlen(result));
			break;
		}
	}
}

客户端:

/*************************************************************************
	> File Name: file_client.c
	> Author: qinf
	> Mail: 
	> Created Time: Sat 05 Apr 2014 09:40:28 AM CST
 ************************************************************************/

#include "file_server.h"
#define MAX_LEN 128
#define OUT 0
#define IN 1
void cmd_handle(char *cmd, char *result);

int main(int argc, char *argv[])
{
	int client_fd, iret;
	struct sockaddr_in server_addr;
	memset(&server_addr, '\0', sizeof(server_addr));
	char cmd[128], result[128];

	char *filenames[MAX_LEN];
	char buf[128];
	char name[128];
	int num = 0, i = 0;
	int fd;
	//sock
	client_fd = socket(AF_INET, SOCK_STREAM, 0);
	if (-1 == client_fd) {
		perror("socket");
		exit(EXIT_FAILURE);
	}
	//connect
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(atoi(argv[2]));
	server_addr.sin_addr.s_addr = inet_addr(argv[1]);
	iret = connect(client_fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
	if (-1 == iret) {
		perror("connect");
		exit(EXIT_FAILURE);
	}
	//send recv
	while (1) {
		memset(cmd, '\0', 128);
		memset(result, '\0', 128);
		fgets(cmd, 128, stdin);
		printf("cmd=%s\n", cmd);
		cmd_handle(cmd, result);
		printf("result=%s\n", result);
		// iret = send(client_fd, cmd, strlen(cmd), 0);
		// if (-1 == iret) {
		// 	perror("send");
		// 	exit(EXIT_FAILURE);
		// }
		num = 0;
		if (strncmp(result, "ls", 2) == 0) {
			iret = send(client_fd, result, strlen(result), 0);
			if (-1 == iret) {
				perror("send");
				exit(EXIT_FAILURE);
			}
			filenames[num] = (char *)malloc(sizeof(char) * 128);
			memset(filenames[num], '\0', 128);
			int n, len;
			recv(client_fd, &n,  sizeof(int), 0);
			printf("n=%d\n", n);
			n = 1;//
			while (--n >= 0) {
				if ((iret = recv(client_fd, filenames[num],  128, 0)) > 0) {
					//printf("%s\n", filenames[num]);
					printf("%s", filenames[num]);
					filenames[++num] = (char *)malloc(sizeof(char) * 128);
					memset(filenames[num], '\0', 128);
				}
			}
		} else if (strncmp(result, "remove", 6) == 0) {
			iret = send(client_fd, result, strlen(result), 0);
			if (-1 == iret) {
				perror("send");
				exit(EXIT_FAILURE);
			}
			recv(client_fd, result,  128, 0);
			printf("%s\n", result);
		} else if (strncmp(result, "get", 3) == 0) {
			// result[strlen(result) - 1] = '\0';
			iret = send(client_fd, result, strlen(result), 0);
			printf("----------result=%s\n", result);
			if (-1 == iret) {
				perror("send");
				exit(EXIT_FAILURE);
			}
			memset(name, '\0', 128);
			memset(buf, '\0', 128);
			printf("name=%s,len=%d\n", result, strlen(result));
			sprintf(name, "../%s", &result[4]);
			printf("new name=%s,len=%d\n", name, strlen(name));
			fd = open(name, O_WRONLY|O_CREAT, 0666);
			if (-1 == fd) {
				perror("open file");
				exit(EXIT_FAILURE);
			}
			//printf("==================\n");
			while((iret = recv(client_fd, buf, 128, 0)) > 0) {
				write(fd, buf, iret);
				memset(buf, '\0', 128);
			}
			close(fd);
			printf("file translate over!\n");
		} else if (strncmp(result, "put", 3) == 0) {
			off_t file_size;
			struct stat file_stat;
			memset(name, '\0', 128);
			memset(buf, '\0', 128);
			printf("result====%s\n", result);
			iret = send(client_fd, result, strlen(result), 0);//发送出错
			printf("name=%s,len=%d\n", result, strlen(result));
			sprintf(name, "%s", &result[4]);
			printf("new name=%s,len=%d\n", name, strlen(name));
			if (-1 == iret) {
				perror("send");
				exit(EXIT_FAILURE);
			}
			iret = stat(name, &file_stat);
			if (-1 == iret) {
				perror("stat");
				exit(EXIT_FAILURE);
			}
			file_size = file_stat.st_size;
			iret = send(client_fd, &file_size, sizeof(file_size), 0);
			if (-1 == iret) {
				perror("send");
				exit(EXIT_FAILURE);
			}
			fd = open(name, O_RDONLY);
			if (-1 == fd) {
				perror("open file");
				exit(EXIT_FAILURE);
			}
			while ((iret = read(fd, buf, 128)) > 0) {
				send(client_fd, buf, iret, 0);
				memset(buf, '\0', 128);
			}
			printf("put over!\n");
		} else if(strncmp(result, "quit", 4) == 0) {
			send(client_fd, result, strlen(result), 0);
			break;
		}
	}
	//close
}

void cmd_handle(char *cmd, char *result) {
	//flag用于去除命令行中间多个space,只保留一个空格,state = OUT代表当前字符为空格
	int i =0, j = 0, ch, state = OUT;
	//处理前置的space
	while((ch = cmd[i++]) != EOF && ch != '\n' && ch != '\0') {
		if (ch == ' ' || ch == '\t') {
			if (state == IN) {
				state = OUT;
				result[j++] = ' ';
			}
		} else if (state == OUT) {
			state = IN;
			result[j++] = ch;
		} else {
			result[j++] = ch;
		}
	}
	result[j] = '\0';
	if (result[j-1] == '\t' || result[j - 1] == ' ')
		result[j-1] = '\0';

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值