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
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <winsock2.h> #include <ws2tcpip.h> #include <stdint.h> #include <direct.h> #include <errno.h> #include <openssl/md5.h>
#pragma comment(lib, "ws2_32.lib") #pragma warning(disable:4996) #define PORT 8080 #define CHUNK_SIZE 1024
void merge_chunks(const char* folder_name, const char* output_file, int total_chunks); void calculate_md5(const char* data, size_t length, unsigned char* md5_result); int compare_md5(const unsigned char* md5_a, const unsigned char* md5_b);
int main() { WSADATA wsa; SOCKET sock; struct sockaddr_in server; char folder_name[] = "chunks"; char file_path[256]; FILE* file; char buffer[CHUNK_SIZE]; unsigned char md5_hash[MD5_DIGEST_LENGTH]; unsigned char received_md5_hash[MD5_DIGEST_LENGTH]; uint32_t chunk_size_net, chunk_size; uint32_t chunk_id_net, chunk_id; int bytes_received; int result; int total_chunks = 0;
printf("Initializing Winsock...\n"); if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { printf("Failed. Error Code: %d", WSAGetLastError()); return 1; } printf("Winsock initialized.\n");
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { printf("Could not create socket: %d", WSAGetLastError()); WSACleanup(); return 1; } printf("Socket created.\n");
server.sin_addr.s_addr = inet_addr("10.0.3.1"); server.sin_family = AF_INET; server.sin_port = htons(PORT);
if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) { printf("Connection failed with error code: %d", WSAGetLastError()); closesocket(sock); WSACleanup(); return 1; } printf("Connected to server.\n");
result = _mkdir(folder_name); if (result == 0) { printf("Directory '%s' created.\n", folder_name); } else if (errno == EEXIST) { printf("Directory '%s' already exists.\n", folder_name); } else { perror("Directory creation failed"); closesocket(sock); WSACleanup(); return 1; }
while (1) { bytes_received = recv(sock, (char*)&chunk_size_net, sizeof(chunk_size_net), 0); if (bytes_received <= 0) { printf("Connection closed or error occurred: %d\n", WSAGetLastError()); break; } chunk_size = ntohl(chunk_size_net);
if (chunk_size == 0) { printf("End of file transmission received.\n"); break; }
bytes_received = recv(sock, (char*)&chunk_id_net, sizeof(chunk_id_net), 0); if (bytes_received <= 0) { printf("Failed to receive chunk ID: %d\n", WSAGetLastError()); break; } chunk_id = ntohl(chunk_id_net);
bytes_received = recv(sock, (char*)received_md5_hash, MD5_DIGEST_LENGTH, 0); if (bytes_received <= 0) { printf("Failed to receive MD5 hash: %d\n", WSAGetLastError()); break; }
snprintf(file_path, sizeof(file_path), "%s/chunk_%03d", folder_name, chunk_id);
file = fopen(file_path, "wb"); if (file == NULL) { perror("File"); closesocket(sock); WSACleanup(); return 1; }
MD5_CTX md5_ctx; MD5_Init(&md5_ctx);
size_t total_bytes_received = 0; while (total_bytes_received < chunk_size) { int bytes_to_receive = (chunk_size - total_bytes_received) > CHUNK_SIZE ? CHUNK_SIZE : (chunk_size - total_bytes_received); bytes_received = recv(sock, buffer, bytes_to_receive, 0); if (bytes_received <= 0) { printf("Data reception failed: %d\n", WSAGetLastError()); fclose(file); closesocket(sock); WSACleanup(); return 1; } fwrite(buffer, sizeof(char), bytes_received, file); total_bytes_received += bytes_received;
MD5_Update(&md5_ctx, buffer, bytes_received); }
MD5_Final(md5_hash, &md5_ctx);
if (compare_md5(md5_hash, received_md5_hash) != 0) { printf("MD5 hash mismatch for chunk %d\n", chunk_id); fclose(file); closesocket(sock); WSACleanup(); return 1; }
printf("Received and saved chunk %d of size %u bytes with matching MD5 hash\n", chunk_id, chunk_size);
fclose(file);
total_chunks++; }
merge_chunks(folder_name, "reconstructed_media_file.mp4", total_chunks);
closesocket(sock); WSACleanup();
return 0; }
void merge_chunks(const char* folder_name, const char* output_file, int total_chunks) { FILE* output; FILE* input; char input_file_path[256]; char buffer[CHUNK_SIZE]; size_t bytes_read;
output = fopen(output_file, "wb"); if (output == NULL) { perror("Failed to open output file"); return; }
for (int i = 0; i < total_chunks; i++) { snprintf(input_file_path, sizeof(input_file_path), "%s/chunk_%03d", folder_name, i); input = fopen(input_file_path, "rb"); if (input == NULL) { perror("Failed to open chunk file"); fclose(output); return; }
while ((bytes_read = fread(buffer, 1, CHUNK_SIZE, input)) > 0) { fwrite(buffer, 1, bytes_read, output); } fclose(input); }
fclose(output); printf("All chunks have been merged into %s.\n", output_file); }
void calculate_md5(const char* data, size_t length, unsigned char* md5_result) { MD5_CTX md5_ctx; MD5_Init(&md5_ctx); MD5_Update(&md5_ctx, data, length); MD5_Final(md5_result, &md5_ctx); }
int compare_md5(const unsigned char* md5_a, const unsigned char* md5_b) { return memcmp(md5_a, md5_b, MD5_DIGEST_LENGTH); }
|