資源簡介
網上很多有關socket文件傳輸的源碼都是每次只能發送一個文件,如果想傳輸多個文件,不但需要多次交互,還需要把每個文件的文件名都輸入進去,很不方便,所以進行了一個小擴展:即客戶端可以將打算傳送的多個文件都先放到一個文件夾目錄下,然后通過代碼實現將這一文件目錄下的所有文件一次打包發送給服務端。該傳輸支持各類型文件,比如圖片、文本等。

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#define?SERVER_PORT?6666
#define?BUFFER_SIZE?1024
#define?CLIENT_PATH?“./client_text/“
int?connect_to_Server(char*?serverIP?int?port)?{
??struct?sockaddr_in?serverAddr;
??int?clientSocket;
??serverAddr.sin_family?=?AF_INET;
??serverAddr.sin_port?=?htons(port);
??serverAddr.sin_addr.s_addr?=?inet_addr(serverIP);
??if((clientSocket?=?socket(AF_INET?SOCK_STREAM?0))?0){
????printf(“sock生成出錯!\n“);
????return?-1;
??}
??if(connect(clientSocket?(struct?sockaddr*)&serverAddr?sizeof(serverAddr))?0)?{
????printf(“connect失敗!\n“);
????return?-1;
??}
??return?clientSocket;
}
int?get_File_Num(char*?pathname)?{
??struct?dirent*?ptr;
??DIR*?path?=?NULL;
??path?=?opendir(pathname);
??int?cnt?=?0;
??while((ptr=readdir(path))?!=?NULL)?{
????if(strcmp(ptr->d_name“.“)==0||strcmp(ptr->d_name“..“)==0)
??????continue;
????if(ptr->d_type==DT_REG)
??????cnt++;
??}
??return?cnt;
}
int?pack_send_File(int?clientSocket?FILE*?fp)?{
??char?send_buf[BUFFER_SIZE]?=?{0};
??int?len_file?len_block;
??int?totalBlock;
??fseek(fp02);//將指針放到文件末尾
??len_file?=?ftell(fp);?//統計文本的數據長度
??rewind(fp);?//讓指針指向文件開始位置
??printf(“文件的總長度len_file:%d\n“len_file);
??totalBlock?=?len_file?%?BUFFER_SIZE?==?0???len_file?/?BUFFER_SIZE?:?((len_file?/?BUFFER_SIZE)?+?1);?//除最后一快的總塊數
??printf(“文件的總塊數totalBlock:%d\n“totalBlock);
??sprintf(send_buf?“%d“?totalBlock);
??send(clientSocket(char*)send_bufsizeof(send_buf)0);
??for(int?i?=?1;?i?<=?totalBlock;?i++)?{
????len_block?=?fread(send_buf?1?BUFFER_SIZE?fp);
????send_buf[len_block]=‘\0‘;
????char?temp[100]?=?{0};
????sprintf(temp?“%d“?len_block);
????printf(“文件第%d塊的長度len_block:%s\n“itemp);
????send(clientSocket(char*)tempsizeof(temp)0);
????printf(“第%d塊的內容:%s\n“isend_buf);
????send(clientSocketsend_buflen_block0);
??}
??fclose(fp);
}
int?submit_File(int?clientSocket)?{//提交單個文件
??//將文件內容發送給服務端
??FILE?*fp;
??char?send_buf[BUFFER_SIZE]?=?{0};
??char?filename[100]?=?{0};
??char*?pre_filename?=?(char*)malloc(sizeof(char)*100);
??while(1){
????strcpy(pre_filenameCLIENT_PATH);
????printf(“請輸入你要提交的文件名:?“);
????scanf(“%[^\n]%*c“filename);
????strcat(pre_filenamefilename);
????fp?=?fopen(pre_filename?“rb“);
????if(fp?==?NULL)?{
??????printf(“您提供的文件不存在,請重新輸入!\n“);
??????continue;
????}
????break;
??}
??printf(“已找到您的文件:路徑是%s\n“pre_filename);
??pack_send_File(clientSocket?fp);
??printf(“文件上傳成功!\n“);
??close(clientSocket);
}
int?submit_Files(int?clientSocket)?{//提交多個文件,想法是提交某個文件夾下的所有文件
??FILE*?fp;
??char?filename[100]?=?{0}?pre_filename[100]?=?{0};
??struct?dirent*?ptr;
??DIR*?path?=?NULL;
??char?send_buf[BUFFER_SIZE]?=?{0};
??path?=?opendir((char*)CLIENT_PATH);
??while((ptr=readdir(path))?!=?NULL)?{
????if(strcmp(ptr->d_name“.“)==0||strcmp(ptr->d_name“..“)==0
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????13568??2020-03-23?19:53??File_Trans\client
?????文件???????4599??2020-03-23?19:47??File_Trans\client.cpp
?????文件???????1174??2020-03-21?21:56??File_Trans\client_text\1
?????文件???????1349??2020-03-23?09:11??File_Trans\client_text\2
?????文件???????1970??2020-03-23?09:14??File_Trans\client_text\3
?????文件???????2533??2020-03-22?16:27??File_Trans\client_text\client.cpp
?????文件????????661??2020-03-21?17:39??File_Trans\client_text\text
?????文件??????30095??2020-03-23?19:49??File_Trans\client_text\timg.jpeg
?????文件??????51381??2020-03-23?19:50??File_Trans\client_text\timg2.jpg
?????文件??????13544??2020-03-23?19:53??File_Trans\server
?????文件???????3939??2020-03-23?19:47??File_Trans\server.cpp
?????目錄??????????0??2020-03-23?20:04??File_Trans\client_text
?????目錄??????????0??2020-03-23?18:16??File_Trans\server_text
?????目錄??????????0??2020-03-23?19:26??File_Trans
-----------?---------??----------?-----??----
???????????????124813????????????????????14
評論
共有 條評論