新日程
这两天主板烧了,所以也没啥过多进度,也就把文件服务器模块设计并解决了,项目各个模块处理完之后顺便把流程图画了。
在做文件服务器模块的时候我发现好多功能和数据库服务器相交了,搞得我有点乱,而且协议改来改去,有些文件也是多模块共用,搞得我都晕了,所以我决定接下来重构。
新甘特图已经备好:
上期日报
来我们看看有多少人坚持下来了:
稍微有点尴尬。。。
应该是忙着赶进度,我这速度是有点飘,所以上天就让我主板烧了躺床上去歇着了吧。
我的进度
预期计划
文件服务器模块解决。
实际情况
解决了。
FTP服务器流程图
FTP操作图
简单放一些零件基类代码,这个后期也要封装成动态库,不面向用户。
//FileBase.h
#pragma once
#include<stdio.h> // printf
#include<stdlib.h> // exit
#include<string.h> // bzero
#include<sys/stat.h>
#include<sys/types.h>
#include<dirent.h>
#include<unistd.h>
#include<netinet/in.h> // sockaddr_in
#include<sys/types.h> // socket
#include<sys/socket.h> // socket
#define SERVER_PORT 20
#define LENGTH_OF_LISTEN_QUEUE 20
#define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512
class FileBase
{
public:
void addFile(char* file_name, int client_socket_fd); //上传文件
int CreateDir(const char* sPathName); //新增目录
void delFile(char* filename); //删除文件
void delDir(char* filename); //删除目录
// void serchDir();查找目录 这个是属于数据库的事情,应该放在包里直接传过来。然后包做一个对内包,不用跟客户端对接的内部协议
void serchlist(char* basePath, char* ret);//查找列表
void serchmode(char* filename);//查找权限
void modDir(const char* old_path, const char* new_path); //修改目录
// void modMode(const char* path, mode_t mode); //权限留在数据库,我才不管
void downFile(char* file_name, int new_server_socket_fd);//下载文件
int connect_to_client();
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
可以看出来其实很多功能当时我是比较纠结放在哪个服务器实现的。
//FileBase.cpp
#include "FileBase.h"
void FileBase::downFile(char* file_name,int new_server_socket_fd) { // ´ò¿ªÎļþ²¢¶ÁÈ¡ÎļþÊý¾Ý FILE* fp = fopen(file_name, "r"); if (NULL == fp) { printf("File:%s Not Found\n", file_name); } else { char buffer[BUFFER_SIZE]; bzero(buffer, BUFFER_SIZE); int length = 0; // ÿ¶ÁÈ¡Ò»¶ÎÊý¾Ý£¬±ã½«Æä·¢Ë͸ø¿Í»§¶Ë£¬Ñ»·Ö±µ½Îļþ¶ÁÍêΪֹ while ((length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0) { if (send(new_server_socket_fd, buffer, length, 0) < 0) { printf("Send File:%s Failed./n", file_name); break; } bzero(buffer, BUFFER_SIZE); } // ¹Ø±ÕÎļþ fclose(fp); printf("File:%s Transfer Successful!\n", file_name); }
} //ÉÏ´«Îļþ
int FileBase::CreateDir(const char* sPathName)
{ char DirName[256]; strcpy(DirName, sPathName); int i, len = strlen(DirName); for (i = 1; i < len; i++) { if (DirName[i] == '/') { DirName[i] = 0; if (access(DirName, NULL) != 0) { if (mkdir(DirName, 0755) == -1) { printf("mkdir error\n"); return -1; } } DirName[i] = '/'; } } return 0;
}
void FileBase::delFile(char* filename) { printf("The file to delete:"); if (remove(filename) == 0) { printf("Removed %s.", filename); } else { perror("remove file"); }
}
void FileBase::delDir(char* filename) { printf("The file to delete:"); if (remove(filename) == 0) { printf("Removed %s.", filename); } else { perror("remove Dir"); }
}
void FileBase::serchlist(char* basePath,char* ret){ DIR* dir; struct dirent* ptr; if ((dir = opendir(basePath)) == NULL) { perror("Open dir error..."); exit(1); } while ((ptr = readdir(dir)) != NULL) { if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) ///current dir OR parrent dir continue; else if (ptr->d_type == 8) { strcat(ret,"File£º"); strcat(ret, ptr->d_name); strcpy(ret, ">"); } ///file else if (ptr->d_type == 10) { strcat(ret, "Link File£º"); strcat(ret, ptr->d_name); strcpy(ret, ">"); } ///link file else if (ptr->d_type == 4) ///dir { strcat(ret, "Dir£º"); strcat(ret, ptr->d_name); strcpy(ret, ">"); } } closedir(dir);
}//²éÕÒÁбí
int FileBase::serchmode(char* filename) { char name[BUFSIZ]; int flag = 0; if (access(name, F_OK) == 0) { printf("Îļþ´æÔÚ\n"); if (access(name,W_OK) == 0) flag += 4; if (access(name, R_OK) == 0) flag += 2; if (access(name, X_OK) == 0) flag += 1; } return flag;
}
void FileBase::modDir(const char* old_path, const char* new_path) { rename(old_path, new_path);
}
//void FileBase::modMode(const char* path, mode_t mode) {
// chmod(path,mode);
//}
void FileBase::addFile(char* file_name, int client_socket_fd) { FILE* fp = fopen(file_name, "w"); if (NULL == fp) { printf("File:\t%s Can Not Open To Write\n", file_name); exit(1); } char buffer[BUFFER_SIZE]; bzero(buffer, BUFFER_SIZE); int length = 0; while ((length = recv(client_socket_fd, buffer, BUFFER_SIZE, 0)) > 0) { if (fwrite(buffer, sizeof(char), length, fp) < length) { printf("File:\t%s Write Failed\n", file_name); break; } bzero(buffer, BUFFER_SIZE); } printf("Receive File:\t%s From Server IP Successful!\n", file_name); close(fp);
}//·¢ËÍÎļþ
int FileBase::connect_to_client() { struct sockaddr_in client_addr; bzero(&client_addr, sizeof(client_addr)); client_addr.sin_family = AF_INET; client_addr.sin_addr.s_addr = htons(INADDR_ANY); client_addr.sin_port = htons(0); int client_socket_fd = socket(AF_INET, SOCK_STREAM, 0); if (client_socket_fd < 0) { perror("Create Socket Failed:"); exit(1); } if (-1 == (bind(client_socket_fd, (struct sockaddr*) & client_addr, sizeof(client_addr)))) { perror("Client Bind Failed:"); exit(1); } struct sockaddr_in server_addr; bzero(&server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; if (inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr) == 0) { perror("Server IP Address Error:"); exit(1); } server_addr.sin_port = htons(SERVER_PORT); socklen_t server_addr_length = sizeof(server_addr); if (connect(client_socket_fd, (struct sockaddr*) & server_addr, server_addr_length) < 0) { perror("Can Not Connect To Server IP:"); exit(0); } return client_socket_fd;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
具体调用就那么用咯。
重构·整体流程
图太长了,截不下来,我就左右分了。
下一工作日任务
重构,将可以封装在动态库里的封装在动态库里,将不能封装在动态库里的,改好之后封装到动态库。
那么,第一步,前置服务器的封装,理想情况是只放出场景类来,GoGoGo!!!
文章来源: lion-wu.blog.csdn.net,作者:看,未来,版权归原作者所有,如需转载,请联系作者。
原文链接:lion-wu.blog.csdn.net/article/details/106982540