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
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <winsock2.h> #include <ws2tcpip.h> #include <windows.h> #include <stdint.h> #include <locale.h>
#pragma comment(lib, "ws2_32.lib") #pragma warning(disable:4996)
#define SERVER_IP "10.0.3.1" #define SERVER_PORT 9527 #define BUFFER_SIZE 1024
void send_all(SOCKET sock, const char* buf, int len) { int sent = 0; while (sent < len) { int res = send(sock, buf + sent, len - sent, 0); if (res <= 0) { fprintf(stderr, "Send error: %d\n", WSAGetLastError()); exit(1); } sent += res; } }
void send_file(SOCKET sock, const wchar_t* path, const char* name) { FILE* file = _wfopen(path, L"rb"); if (!file) { perror("File open failed"); return; }
fseek(file, 0, SEEK_END); uint64_t size = _ftelli64(file); fseek(file, 0, SEEK_SET);
uint32_t name_len = strlen(name); uint32_t net_len = htonl(name_len); send_all(sock, (char*)&net_len, 4); send_all(sock, name, name_len);
uint64_t net_size = htonll(size); send_all(sock, (char*)&net_size, 8);
char buffer[BUFFER_SIZE]; size_t bytes_read; while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) > 0) { send_all(sock, buffer, bytes_read); }
fclose(file); }
void send_directory(SOCKET sock, const wchar_t* directory) { wchar_t search_path[MAX_PATH]; _snwprintf(search_path, MAX_PATH, L"%s\\*", directory);
WIN32_FIND_DATAW find_data; HANDLE hFind = FindFirstFileW(search_path, &find_data); if (hFind == INVALID_HANDLE_VALUE) { perror("FindFirstFile failed"); return; }
do { if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue; if (wcscmp(find_data.cFileName, L".") == 0 || wcscmp(find_data.cFileName, L"..") == 0) continue;
wchar_t filepath[MAX_PATH]; _snwprintf(filepath, MAX_PATH, L"%s\\%s", directory, find_data.cFileName);
char linux_name[MAX_PATH]; WideCharToMultiByte(CP_UTF8, 0, find_data.cFileName, -1, linux_name, MAX_PATH, NULL, NULL); for (char* p = linux_name; *p; ++p) { if (*p == '\\') *p = '/'; }
send_file(sock, filepath, linux_name); } while (FindNextFileW(hFind, &find_data));
FindClose(hFind); }
int main() { setlocale(LC_ALL, "");
WSADATA wsaData; SOCKET sockfd; struct sockaddr_in serverAddr;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { printf("Winsock初始化失败。\n"); return 1; }
sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == INVALID_SOCKET) { printf("创建套接字失败。\n"); WSACleanup(); return 1; }
serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(SERVER_PORT); inet_pton(AF_INET, SERVER_IP, &serverAddr.sin_addr);
if (connect(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) { fprintf(stderr, "Connect failed: %d\n", WSAGetLastError()); closesocket(sockfd); WSACleanup(); return 1; }
const wchar_t* folder = L"files"; char folder_mb[MAX_PATH]; WideCharToMultiByte(CP_UTF8, 0, folder, -1, folder_mb, MAX_PATH, NULL, NULL);
uint32_t folder_len = strlen(folder_mb) + 1; uint32_t net_folder_len = htonl(folder_len); send_all(sockfd, (char*)&net_folder_len, 4); send_all(sockfd, folder_mb, folder_len);
send_directory(sockfd, folder);
printf("文件夹发送成功。\n");
closesocket(sockfd); WSACleanup();
return 0; }
|