分割传输视频流

一、C语言分割传输文件

1.LinServer.c

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
// server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // For close()
#include <arpa/inet.h> // For inet_addr()
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>

#define PORT 8080
#define CHUNK_SIZE 1024 // Adjust as needed

int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char *filename = "copy.mp4"; // Replace with your media file
FILE *file;
char buffer[CHUNK_SIZE];
int chunk_id = 0;
size_t n;

// Create socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Socket failed");
exit(EXIT_FAILURE);
}

// Prepare the sockaddr_in structure
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY; // Accept any IP address
address.sin_port = htons(PORT);

// Bind
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("Bind failed");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("Server bound to port %d\n", PORT);

// Listen
if (listen(server_fd, 3) < 0) {
perror("Listen");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("Server listening...\n");

// Accept incoming connection
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t *)&addrlen)) < 0) {
perror("Accept");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("Connection accepted\n");

// Open file
file = fopen(filename, "rb");
if (file == NULL) {
perror("File not found");
close(new_socket);
close(server_fd);
exit(EXIT_FAILURE);
}

// Send file in chunks
while ((n = fread(buffer, sizeof(char), CHUNK_SIZE, file)) > 0) {
// Send chunk metadata: chunk size and chunk ID
uint32_t chunk_size_net = htonl(n); // Convert to network byte order
uint32_t chunk_id_net = htonl(chunk_id);

// Send chunk size
if (send(new_socket, &chunk_size_net, sizeof(chunk_size_net), 0) < 0) {
perror("Send chunk size");
fclose(file);
close(new_socket);
close(server_fd);
exit(EXIT_FAILURE);
}

// Send chunk ID
if (send(new_socket, &chunk_id_net, sizeof(chunk_id_net), 0) < 0) {
perror("Send chunk ID");
fclose(file);
close(new_socket);
close(server_fd);
exit(EXIT_FAILURE);
}

// Send chunk data
if (send(new_socket, buffer, n, 0) < 0) {
perror("Send chunk data");
fclose(file);
close(new_socket);
close(server_fd);
exit(EXIT_FAILURE);
}

printf("Sent chunk %d of size %zu bytes\n", chunk_id, n);
chunk_id++;
}

// Send a signal to indicate the end of transmission
uint32_t end_signal = htonl(0); // Chunk size 0 indicates end
send(new_socket, &end_signal, sizeof(end_signal), 0);

printf("File sent successfully in chunks.\n");

// Clean up
fclose(file);
close(new_socket);
close(server_fd);

return 0;
}

LinServer编译脚本makefile

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
# Makefile for compiling the server code

# Compiler
CC = gcc

# Compiler flags
CFLAGS = -Wall -O2

# Libraries to link with
LIBS = -lssl -lcrypto -lavformat -lavcodec -lavutil

# Output executable name
TARGET = LinServer

# Source files
SRCS = LinServer.c

# Object files
OBJS = $(SRCS:.c=.o)

# Default target
all: $(TARGET)

# Rule for linking the executable
$(TARGET): $(OBJS)
$(CC) -o $(TARGET) $(OBJS) $(LIBS)

# Rule for compiling source files
%.o: %.c
$(CC) -c $< -o $@ $(CFLAGS)

# Clean up object files and executable
clean:
rm -f $(OBJS) $(TARGET)

.PHONY: all clean

2.WinClient.c

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
// client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h> // Winsock library
#include <ws2tcpip.h>
#include <stdint.h>

#pragma comment(lib, "ws2_32.lib") // Link with Winsock library
#pragma warning(disable:4996)
#define PORT 8080
#define CHUNK_SIZE 1024 // Should match the server's CHUNK_SIZE

int main() {
WSADATA wsa;
SOCKET sock;
struct sockaddr_in server;
char folder_name[] = "chunks"; // Folder to save chunks
char file_path[256];
FILE* file;
char buffer[CHUNK_SIZE];
uint32_t chunk_size_net, chunk_size;
uint32_t chunk_id_net, chunk_id;
int bytes_received;
int result;

// Initialize Winsock
printf("Initializing Winsock...\n");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("Failed. Error Code: %d", WSAGetLastError());
return 1;
}
printf("Winsock initialized.\n");

// Create socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
printf("Could not create socket: %d", WSAGetLastError());
WSACleanup();
return 1;
}
printf("Socket created.\n");

// Prepare server address
server.sin_addr.s_addr = inet_addr("10.0.3.1"); // Replace with server IP
server.sin_family = AF_INET;
server.sin_port = htons(PORT);

// Connect to server
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");

// Create directory for chunks
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;
}

// Receive chunks
while (1) {
// Receive chunk size
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);

// Check for end signal
if (chunk_size == 0) {
printf("End of file transmission received.\n");
break;
}

// Receive chunk ID
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);

// Build file path for chunk
snprintf(file_path, sizeof(file_path), "%s/chunk_%d", folder_name, chunk_id);

// Open file to write chunk
file = fopen(file_path, "wb");
if (file == NULL) {
perror("File");
closesocket(sock);
WSACleanup();
return 1;
}

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;
}

printf("Received and saved chunk %d of size %u bytes\n", chunk_id, chunk_size);

// Close the file
fclose(file);
}

// Cleanup
closesocket(sock);
WSACleanup();

return 0;
}

二、分隔传输后在客户端合成

1. Linserver

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
// server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <openssl/md5.h>

#define PORT 8080
#define CHUNK_SIZE 1024

void calculate_md5(const char *data, size_t length, unsigned char *md5_result);

int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char *filename = "落叶的位置.mp4"; // Replace with your media file
FILE *file;
char buffer[CHUNK_SIZE];
unsigned char md5_hash[MD5_DIGEST_LENGTH];
int chunk_id = 0;
size_t n;

// Create socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Socket failed");
exit(EXIT_FAILURE);
}

// Prepare the sockaddr_in structure
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY; // Accept any IP address
address.sin_port = htons(PORT);
int optval = 1;
// 设置 SO_REUSEADDR
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
perror("setsockopt SO_REUSEADDR failed");
}
// Bind
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("Bind failed");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("Server bound to port %d\n", PORT);

// Listen
if (listen(server_fd, 3) < 0) {
perror("Listen");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("Server listening...\n");

// Accept incoming connection
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t *)&addrlen)) < 0) {
perror("Accept");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("Connection accepted\n");

// Open file
file = fopen(filename, "rb");
if (file == NULL) {
perror("File not found");
close(new_socket);
close(server_fd);
exit(EXIT_FAILURE);
}

// Send file in chunks
while ((n = fread(buffer, sizeof(char), CHUNK_SIZE, file)) > 0) {
// Calculate MD5 hash of the chunk
calculate_md5(buffer, n, md5_hash);

// Send chunk metadata: chunk size, chunk ID, and MD5 hash
uint32_t chunk_size_net = htonl(n); // Convert to network byte order
uint32_t chunk_id_net = htonl(chunk_id);

// Send chunk size
if (send(new_socket, &chunk_size_net, sizeof(chunk_size_net), 0) < 0) {
perror("Send chunk size");
fclose(file);
close(new_socket);
close(server_fd);
exit(EXIT_FAILURE);
}

// Send chunk ID
if (send(new_socket, &chunk_id_net, sizeof(chunk_id_net), 0) < 0) {
perror("Send chunk ID");
fclose(file);
close(new_socket);
close(server_fd);
exit(EXIT_FAILURE);
}

// Send MD5 hash
if (send(new_socket, md5_hash, MD5_DIGEST_LENGTH, 0) < 0) {
perror("Send MD5 hash");
fclose(file);
close(new_socket);
close(server_fd);
exit(EXIT_FAILURE);
}

// Send chunk data
if (send(new_socket, buffer, n, 0) < 0) {
perror("Send chunk data");
fclose(file);
close(new_socket);
close(server_fd);
exit(EXIT_FAILURE);
}

printf("Sent chunk %d of size %zu bytes with MD5 hash\n", chunk_id, n);
chunk_id++;
}

// Send a signal to indicate the end of transmission
uint32_t end_signal = htonl(0); // Chunk size 0 indicates end
send(new_socket, &end_signal, sizeof(end_signal), 0);

printf("File sent successfully in chunks.\n");

// Clean up
fclose(file);
close(new_socket);
close(server_fd);

return 0;
}

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);
}

LinServer编译脚本makefile

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
# Makefile for compiling the server code

# Compiler
CC = gcc

# Compiler flags
CFLAGS = -Wall -O2

# Libraries to link with
LIBS = -lssl -lcrypto -lavformat -lavcodec -lavutil

# Output executable name
TARGET = LinServer

# Source files
SRCS = LinServer.c

# Object files
OBJS = $(SRCS:.c=.o)

# Default target
all: $(TARGET)

# Rule for linking the executable
$(TARGET): $(OBJS)
$(CC) -o $(TARGET) $(OBJS) $(LIBS)

# Rule for compiling source files
%.o: %.c
$(CC) -c $< -o $@ $(CFLAGS)

# Clean up object files and executable
clean:
rm -f $(OBJS) $(TARGET)

.PHONY: all clean

2.Winclient.c

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
// client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h> // Winsock library
#include <ws2tcpip.h>
#include <stdint.h>
#include <direct.h> // For _mkdir()
#include <errno.h>
#include <openssl/md5.h>

#pragma comment(lib, "ws2_32.lib") // Link with Winsock library
#pragma warning(disable:4996)
#define PORT 8080
#define CHUNK_SIZE 1024 // Should match the server's CHUNK_SIZE

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"; // Folder to save 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;

// Initialize Winsock
printf("Initializing Winsock...\n");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("Failed. Error Code: %d", WSAGetLastError());
return 1;
}
printf("Winsock initialized.\n");

// Create socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
printf("Could not create socket: %d", WSAGetLastError());
WSACleanup();
return 1;
}
printf("Socket created.\n");

// Prepare server address
server.sin_addr.s_addr = inet_addr("10.0.3.1"); // Replace with server IP
server.sin_family = AF_INET;
server.sin_port = htons(PORT);

// Connect to server
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");

// Create directory for chunks
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;
}

// Receive chunks
while (1) {
// Receive chunk size
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);

// Check for end signal
if (chunk_size == 0) {
printf("End of file transmission received.\n");
break;
}

// Receive chunk ID
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);

// Receive MD5 hash
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;
}

// Build file path for chunk with leading zeros
snprintf(file_path, sizeof(file_path), "%s/chunk_%03d", folder_name, chunk_id);

// Open file to write chunk
file = fopen(file_path, "wb");
if (file == NULL) {
perror("File");
closesocket(sock);
WSACleanup();
return 1;
}

// Initialize MD5 context
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;

// Update MD5 hash with the received data
MD5_Update(&md5_ctx, buffer, bytes_received);
}

// Finalize MD5 hash calculation
MD5_Final(md5_hash, &md5_ctx);

// Compare MD5 hashes
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);

// Close the file
fclose(file);

// Increment total_chunks
total_chunks++;
}

// Merge chunk files into one
merge_chunks(folder_name, "reconstructed_media_file.mp4", total_chunks);

// Cleanup
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);
}