-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathtransfer.c
63 lines (42 loc) · 1.33 KB
/
transfer.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
//
// transfer.c
// Algorithms - Network transfer
//
// Created by YourtionGuo on 18/05/2017.
// Copyright © 2017 Yourtion. All rights reserved.
//
#include <sys/types.h>
#include <sys/socket.h>
#include "compress.h"
#include "transfer.h"
#pragma mark - Public
int send_comp(int s, const unsigned char *data, int size, int flags)
{
unsigned char *compressed;
int size_comp;
/// 压缩数据
if ((size_comp = huffman_compress(data, &compressed, size)) < 0) return -1;
/// 发送压缩数据及大小
if (send(s, (char *)&size_comp, sizeof(int), flags) != sizeof(int)) return -1;
if (send(s, (char *)compressed, size_comp, flags) != size_comp) return -1;
/// 释放已压缩数据
free(compressed);
return 0;
}
int recv_comp(int s, unsigned char **data, int *size, int flags)
{
unsigned char *compressed;
int size_comp;
/// 根据数据大小接收压缩数据
if (recv(s, (char *)&size_comp, sizeof(int), flags) != sizeof(int)) return -1;
if ((compressed = (unsigned char *)malloc(size_comp)) == NULL) return -1;
if (recv(s, (char *)compressed, size_comp, flags) != size_comp) {
free(compressed);
return -1;
}
/// 解压数据
if ((*size = huffman_uncompress(compressed, data)) < 0) return -1;
/// 释放收到的压缩数据
free(compressed);
return 0;
}