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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
| #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <string> #include <climits> #include <algorithm> #include <queue> #include <vector> #include <netinet/in.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #include <fstream> const int BACKLOG=2; const int CONCURRENT_MAX=3; const int BUFFER_SIZE=1024; char input_msg[BUFFER_SIZE]; char recv_msg[BUFFER_SIZE];
class Socket_server{ public: Socket_server(){ server_sock_fd=0; tv.tv_sec = 20; tv.tv_usec = 0; max_fd=-1; memset(client_fds,0,sizeof(client_fds)); memset(client_to,-1,sizeof(client_to)); filename.clear(); path.clear(); }; ~Socket_server(){
}; bool init(const int port); bool start(); bool sendmessage(); bool sendIP(int index); void recv_and_forward(); void recv_word(int index,int byte_num); void recv_file(int index,int byte_num); void recv_video(int index); void accept_new(); private: int server_sock_fd; fd_set server_fd_set; int max_fd; timeval tv; int client_fds[CONCURRENT_MAX+1]; struct sockaddr_in client_addr[CONCURRENT_MAX+1]; int client_to[CONCURRENT_MAX+1]; std::string filename; std::string path; }; bool Socket_server::init(const int port){ struct sockaddr_in server_addr; server_addr.sin_len = sizeof(struct sockaddr_in); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(&(server_addr.sin_zero),0,8); server_sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_sock_fd == -1){ perror("socket error"); return false; }
int on = 1; if(setsockopt(server_sock_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))==-1){ perror("setsockopt"); return false; }
int bind_result = bind(server_sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (bind_result == -1){ perror("bind error"); return false; }
if (listen(server_sock_fd, BACKLOG) == -1){ perror("listen error"); return false; } printf("欢迎使用SOCKET通讯系统,输入quit并确认可退出\n");
return true; } bool Socket_server::start() { FD_ZERO(&server_fd_set); FD_SET(STDIN_FILENO, &server_fd_set);
if (max_fd < STDIN_FILENO) max_fd = STDIN_FILENO; FD_SET(server_sock_fd, &server_fd_set); if (max_fd < server_sock_fd){ max_fd = server_sock_fd; } for (int i = 1; i <= CONCURRENT_MAX; i++){ if (client_fds[i]!=0){ FD_SET(client_fds[i], &server_fd_set); if (max_fd < client_fds[i]) max_fd = client_fds[i]; } } int ret = select(max_fd+1, &server_fd_set, NULL, NULL, &tv); if (ret < 0){ perror("select 出错"); return false; } else if(ret == 0){ printf("timeout...\n"); return false; } return true; } bool Socket_server::sendmessage() { if (FD_ISSET(STDIN_FILENO, &server_fd_set)){ memset(input_msg,0,sizeof(input_msg)); fgets(input_msg, BUFFER_SIZE, stdin); char *find = strchr(input_msg, '\n'); if(find) *find = '\0'; if (strcmp(input_msg, "\\quit") == 0){ shutdown(server_sock_fd,SHUT_RDWR); return false; } if(input_msg[0]=='1' && input_msg[1]==' '){ for (int i=1; i<=CONCURRENT_MAX; i++){ if (client_fds[i]!=0){ std::string s; s.clear(); s="0"; if(send(client_fds[i],s.c_str(),BUFFER_SIZE,0)==-1) perror("发送消息出错!"); if(send(client_fds[i], input_msg, BUFFER_SIZE, 0)==-1) perror("发送消息出错!"); } } } else if(input_msg[0]=='2' && input_msg[1]==' '){ for (int i=1; i<=CONCURRENT_MAX; i++){ if(client_fds[i]!=0){ std::string s; s.clear(); s="0"; if(send(client_fds[i],s.c_str(),BUFFER_SIZE,0)==-1) perror("转发消息出错!"); } } for (int i=1; i<=CONCURRENT_MAX; i++){ if(client_fds[i]!=0){ if(send(client_fds[i],input_msg,BUFFER_SIZE,0)<0) perror("发送消息出错!"); } } path.clear(); for (int i=2; input_msg[i]!='\n' && input_msg[i]!='\0'; i++){ path+=input_msg[i]; } FILE *fp=fopen(path.c_str(),"rb+"); char chtemp[BUFFER_SIZE+1]; memset(chtemp,'\0',sizeof(chtemp)); int cnt=0; if(fp==nullptr){ printf("文件无法打开\n"); } else { while((cnt=fread(chtemp,sizeof(char),BUFFER_SIZE,fp))>0){ for (int i=1; i<=CONCURRENT_MAX; i++){ if(client_fds[i]!=0){ if(send(client_fds[i],chtemp,cnt,0)<0){ perror("发送消息出错!"); } } } memset(chtemp,'\0',sizeof(chtemp)); } fclose(fp); printf("Transfer Successful!\n"); } path.clear(); filename.clear(); } else if(input_msg[0]=='3' && input_msg[1]==' '){ printf("敬请期待!\n"); }
} return true; } bool Socket_server::sendIP(int index) { std::string s1="0"; send(client_fds[index],s1.c_str(),BUFFER_SIZE,0); s1.clear();
s1="1 您现在是"+std::to_string(index)+"号客户机, 当前聊天室内成员如下:\n"; send(client_fds[index],s1.c_str(),BUFFER_SIZE,0); for (int i=1; i<=CONCURRENT_MAX; i++){ if(client_fds[i]!=0){ s1.clear(); s1="0"; send(client_fds[index],s1.c_str(),BUFFER_SIZE,0); s1.clear(); s1="1 "+std::to_string(i)+"号客户机,IP地址为:"+inet_ntoa(client_addr[i].sin_addr)+"\n"; if(send(client_fds[index],s1.c_str(),BUFFER_SIZE,0)==-1) return false; } if(client_fds[i]!=0 && i!=index){ s1.clear(); s1="0"; send(client_fds[i],s1.c_str(),BUFFER_SIZE,0); s1.clear(); s1="1 新加入了"+std::to_string(index)+"号客户机,IP地址为:"+inet_ntoa(client_addr[index].sin_addr)+"\n"; if(send(client_fds[i],s1.c_str(),BUFFER_SIZE,0)==-1) return false; } } return true; } void Socket_server::recv_word(int index,int byte_num) { if(client_to[index]==0 || client_to[index]==-2){ printf("客户端(%d):",index); for (int j=2; j<byte_num; j++){ if(recv_msg[j]=='\0') break; printf("%c",recv_msg[j]); } printf("\n"); } if(client_to[index]==-2){ for (int i=1; i<=CONCURRENT_MAX; i++){ if(client_fds[i]!=0 && i!=index){ std::string s; s.clear(); s=std::to_string(index); if(send(client_fds[i],s.c_str(),BUFFER_SIZE,0)==-1) perror("转发消息出错!"); if (send(client_fds[i], recv_msg, BUFFER_SIZE, 0) == -1){ perror("转发消息出错!"); } } } } else if(client_to[index]!=0){ std::string s; s.clear(); s=std::to_string(index); if(send(client_fds[client_to[index]],s.c_str(),BUFFER_SIZE,0)==-1) perror("转发消息出错!"); if (send(client_fds[client_to[index]], recv_msg, BUFFER_SIZE, 0) == -1){ perror("转发消息出错!"); } } } void Socket_server::recv_file(int index,int byte_num) { std::string tempfilename; tempfilename.clear(); if(client_to[index]==0 || client_to[index]==-2){ int pos=0; for (int j=byte_num-1; j>0; j--){ if(recv_msg[j]=='/' || recv_msg[j]=='\\'){ pos=j; break; } } for (int j=pos+1; j<byte_num; j++){ if(recv_msg[j]=='\0') break; filename+=recv_msg[j]; } printf("客户端(%d)向你传送了一个文件,请输入您想要存放的绝对路径,例如:/Users/longzhengtian/Desktop/\n注意,最后要有一个'/'符号,如果没有,系统将自动填充,请输入:",index); std::cin>>path; getchar(); if(path[path.size()-1]!='/'){ path+="/"; } path+=filename; FILE *fp=fopen(path.c_str(),"wb+"); if(fp==nullptr) printf("接收文件出错!\n"); else { memset(recv_msg,0,sizeof(recv_msg)); int cnt=0; while((cnt=recv(client_fds[index],recv_msg,BUFFER_SIZE,0)) && cnt>0){ if(fwrite(recv_msg,sizeof(char),cnt,fp)<cnt){ printf("接收文件出错!\n"); break; } memset(recv_msg,0,sizeof(recv_msg)); if(cnt!=BUFFER_SIZE) break; } printf("Receive successful!\n"); fclose(fp); } if(client_to[index]==0) path.clear(); filename.clear(); } if(client_to[index]==-2){ for (int i=1; i<=CONCURRENT_MAX; i++){ if(client_fds[i]!=0 && i!=index){ std::string s; s.clear(); s=std::to_string(index); if(send(client_fds[i],s.c_str(),BUFFER_SIZE,0)==-1) perror("转发消息出错!"); } } filename="2 "+path; for (int i=1; i<=CONCURRENT_MAX; i++){ if(i!=index && client_fds[i]!=0){ if(send(client_fds[i],filename.c_str(),BUFFER_SIZE,0)<0) perror("发送消息出错!"); } } FILE *fp=fopen(path.c_str(),"rb+"); char chtemp[BUFFER_SIZE+1]; memset(chtemp,'\0',sizeof(chtemp)); int cnt=0; if(fp==nullptr){ printf("文件无法打开\n"); } else { while((cnt=fread(chtemp,sizeof(char),BUFFER_SIZE,fp))>0){ for (int i=1; i<=CONCURRENT_MAX; i++){ if(i!=index && client_fds[i]!=0){ if(send(client_fds[i],chtemp,cnt,0)<0){ perror("发送消息出错!"); } } } memset(chtemp,'\0',sizeof(chtemp)); } fclose(fp); printf("Transfer Successful!\n"); } path.clear(); filename.clear(); } else if(client_to[index]!=0){ std::string s; s.clear(); s=std::to_string(index); if(send(client_fds[client_to[index]],s.c_str(),BUFFER_SIZE,0)==-1) perror("转发文件出错!"); if(send(client_fds[client_to[index]],recv_msg,BUFFER_SIZE,0)==-1) perror("转发文件出错!");
memset(recv_msg,0,sizeof(recv_msg)); int cnt=0; while((cnt=recv(client_fds[index],recv_msg,BUFFER_SIZE,0)) && cnt>0){ if(send(client_fds[client_to[index]],recv_msg,cnt,0)==-1) perror("转发文件出错!"); memset(recv_msg,0,sizeof(recv_msg)); if(cnt!=BUFFER_SIZE) break; } printf("A file from Client%d to Client%d was successfully forwarded through this server\n",index,client_to[index]); } } void Socket_server::recv_video(int index) { printf("敬请期待!\n"); } void Socket_server::recv_and_forward() { for (int i = 1; i <= CONCURRENT_MAX; i++){ if (client_fds[i]!=0){ if (FD_ISSET(client_fds[i], &server_fd_set)){ memset(recv_msg,0,sizeof(recv_msg)); int byte_num = recv(client_fds[i],recv_msg,BUFFER_SIZE,0); if (byte_num > 0){ if (byte_num > BUFFER_SIZE) byte_num = BUFFER_SIZE; recv_msg[byte_num] = '\0'; if(strcmp(recv_msg,"\\exit")==0){ client_to[i]=-1; } else if(client_to[i]==-1){ if(recv_msg[0]=='-') client_to[i]=-2; else client_to[i]=recv_msg[0]-'0'; } else if(recv_msg[0]=='1'){ recv_word(i,byte_num); } else if(recv_msg[0]=='2'){ recv_file(i,byte_num); } else if(recv_msg[0]=='3'){ recv_video(i); } } else if(byte_num < 0) printf("从客户端(%d)接受消息出错.\n",i); else{ FD_CLR(client_fds[i], &server_fd_set); client_fds[i] = 0; printf("客户端(%d)退出了\n",i); std::string s1; for (int j=1; j<=CONCURRENT_MAX; j++){ if(client_fds[j]!=0 && i!=j){ s1.clear(); s1=std::to_string(i)+"号客户机退出了!\n"; send(client_fds[j],s1.c_str(),BUFFER_SIZE,0); } } } } } } } void Socket_server::accept_new() { if (FD_ISSET(server_sock_fd, &server_fd_set)){ struct sockaddr_in client_address; socklen_t address_len=sizeof(struct sockaddr_in); int client_socket_fd = accept(server_sock_fd, (struct sockaddr *)&client_address, &address_len);
if (client_socket_fd > 0){ int index = -1; for (int i = 1; i <= CONCURRENT_MAX; i++){ if (client_fds[i] == 0){ index = i; client_fds[i] = client_socket_fd; break; } } if (index >= 0){ printf("新客户端(%d)加入成功 %s:%d \n",index,inet_ntoa(client_address.sin_addr),ntohs(client_address.sin_port)); client_addr[index]=client_address; sendIP(index); } else{ memset(input_msg,0,sizeof(input_msg)); strcpy(input_msg, "服务器加入的客户端数达到最大值!\n"); send(client_socket_fd, input_msg, BUFFER_SIZE, 0); printf("客户端连接数达到最大值,新客户端加入失败 %s:%d \n",inet_ntoa(client_address.sin_addr),ntohs(client_address.sin_port)); } } } } int main (){ Socket_server tcpserver; tcpserver.init(11111);
while (true){ if(!tcpserver.start()) { continue; } else{ tcpserver.accept_new(); if(!tcpserver.sendmessage()) exit(0); tcpserver.recv_and_forward(); } } return 0; }
|