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 206
| #include <netdb.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #include <error.h> #include <pthread.h> #include <semaphore.h> #include <bits/stdc++.h> #define BUFLEN 20000 #define FILE_SIZE 1001000 #define FILE_NAME_LEN 300 using namespace std; map<string, int> mp; char dest_folder[100]="./new"; ofstream out; sem_t my_semaphore;
struct FileStruct { char fileName[300]; long long fileSize; };
long long getFileSize(char *fileName) { FILE *fp = fopen(fileName, "r"); if(fp==NULL)return -1; fseek(fp, 0, SEEK_END); size_t size = ftell(fp); fclose(fp); return size; }
char *getFileName(char *pathName) { char* tem=pathName; while (*tem != '\0') tem++; while (*tem != '/' && tem!=pathName) tem--; return tem==pathName?tem:tem+1; }
void input(char *buf) { fgets(buf, BUFLEN, stdin); int len = strlen(buf); buf[len - 1] = '\0'; }
void Unpack(struct FileStruct *fileStruct, char *file_content, char *folder_name) { char Full_name[FILE_NAME_LEN]; sprintf(Full_name, "%s/%s", folder_name, fileStruct->fileName); FILE *file = fopen(Full_name, "wb"); fwrite(file_content, sizeof(char), fileStruct->fileSize, file); return; }
void add_suffix(char *new_filename, int occur_time, char *old_filename) { char *tem = old_filename; while (*tem != '\0') tem++; while (*tem != '.' && tem != old_filename) tem--; if (tem != old_filename) { *tem = '\0'; sprintf(new_filename, "%s(%d).%s", old_filename, occur_time, tem + 1); *tem = '.'; return; } else { sprintf(new_filename, "%s(%d)", old_filename, occur_time); return; } }
void *client_recv(void *in) { int my_sock = (long)in; char *buf = (char *)malloc((BUFLEN + 1) * sizeof(char)); while (1) { int c1 = recv(my_sock, buf, BUFSIZ, 0); if (c1 == -1 || c1 == 0) { printf("接收消息错误!\n"); return NULL; } if (c1 <= 300) { buf[c1] = '\0'; printf("收到消息:\n%s\n", buf); } else { char file_name[FILE_NAME_LEN]; char full_file_name[FILE_NAME_LEN]; memcpy(file_name, buf, FILE_NAME_LEN);
sem_wait(&my_semaphore); if (mp.count(string(file_name))) { char new_file_name[FILE_NAME_LEN]; add_suffix(new_file_name, mp[string(file_name)], file_name); strcpy(file_name, new_file_name); } sprintf(full_file_name, "%s/%s", dest_folder, file_name); mp[string(file_name)]++; sem_post(&my_semaphore); out.open(full_file_name, ios::out);
out.write(buf + sizeof(FileStruct), strlen(buf + sizeof(FileStruct))); printf("文件已经被写入到 %s!\n", full_file_name); out.close(); } } }
int main(int argc, char *argv[]) { const char *host = "127.0.0.1"; char *service = "50500"; struct sockaddr_in sin, fsin; char buf[BUFLEN + 1]; int sock, ssock, alen; int cc; pthread_t thread_handle; sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = inet_addr(host); sin.sin_port = htons((unsigned short)atoi(service));
bind(sock, (struct sockaddr *)&sin, sizeof(sin));
listen(sock, 5);
ssock = accept(sock, (struct sockaddr *)&fsin, (socklen_t *)&alen);
sem_init(&my_semaphore, 0, 1); pthread_create(&thread_handle, NULL, client_recv, (void *)ssock);
while (1) { input(buf);
if (strcmp(buf, ">quit") == 0) { printf("退出成功!\n"); break; }
else if (strncmp(">chat", buf, 5) == 0) { send(ssock, buf + 6, strlen(buf + 6), 0); printf("消息发送成功!\n"); }
else if (strncmp(">send", buf, 5) == 0) { struct FileStruct f; f.fileSize = getFileSize(buf + 6); if(f.fileSize==-1){ cout<<"文件名错误!\n"; continue; } strcpy(f.fileName, getFileName(buf + 6)); char *File_pack = (char *)malloc(sizeof(char) * FILE_SIZE); memcpy(File_pack, &f, sizeof(FileStruct)); FILE *tem_file = fopen(buf + 6, "r"); char *content = (char *)malloc(f.fileSize); fread(content, f.fileSize, 1, tem_file); fclose(tem_file); memcpy(File_pack + sizeof(FileStruct), content, f.fileSize); send(ssock, File_pack, sizeof(FileStruct) + f.fileSize, 0); printf("文件发送成功!\n"); }
else if (strncmp(">rdir", buf, 5) == 0) { sem_wait(&my_semaphore); strcpy(dest_folder, buf + 6); mp.clear(); sem_post(&my_semaphore); printf("文件夹路径已被修改!\n"); } }
pthread_join(thread_handle, NULL); close(sock); printf("按回车键继续..."); getchar(); }
|