-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_client_functions.cc
125 lines (98 loc) · 2.89 KB
/
server_client_functions.cc
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
#include "server_client_functions.h"
#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstdio>
long get_file_size(std::FILE* ptr){
long size;
fseek(ptr, 0, SEEK_END);
size = ftell(ptr);
fseek(ptr, 0, SEEK_SET);
return size;
}
void receive_image(int clientfd, const char* save_path){
char buffer[BUFFER_SIZE];
long file_size, total_bytes_received = 0, bytes_received = 0;
FILE* fd;
//receive the file size from client
bytes_received = recv(clientfd, &file_size, sizeof(file_size), 0);
if(bytes_received == -1){
std::cerr<<"error receiving file size...";
exit(EXIT_FAILURE);
}
// convert the file size to host byte order to be able to use it
file_size = ntohl(file_size);
// open the file for writing
fd = fopen(save_path, "wb");
if (fd == NULL ){
std::cerr << "Error creating the file...";
return;
}
// receive file data in chunks
while(total_bytes_received < file_size){
bytes_received = recv(clientfd, buffer, sizeof(buffer), 0);
if(bytes_received <= 0) {
if(bytes_received ==0){
std::cerr << "Connection lost.";
}
else{
std::cerr << "Error receiving file";
return;
}
return;
}
// write the received data to the file
fwrite(buffer, 1, bytes_received, fd);
total_bytes_received += bytes_received;
}
// free the resource
fclose(fd);
}
void send_image(int serverfd, const char* image) {
std::FILE* fd;
char buffer[BUFFER_SIZE];
long file_size;
uint32_t network_file_size;
ssize_t bytes_sent = 0, total_bytes = 0;
size_t bytes_read;
fd = fopen(image, "rb");
if (fd == NULL) {
std::cerr << "Could not read the file " << image;
return;
}
file_size = get_file_size(fd);
network_file_size = htonl((uint32_t)file_size);
if (send(serverfd, &network_file_size, sizeof(network_file_size), 0) != sizeof(network_file_size)) {
std::cerr << "Error sending file size";
fclose(fd);
return;
}
while (total_bytes < file_size) {
bytes_read = fread(buffer, 1, BUFFER_SIZE, fd);
if (bytes_read == 0) {
if (ferror(fd)) {
std::cerr << "Error reading file";
fclose(fd);
return;
}
break; // End of file
}
ssize_t bytes_to_send = bytes_read;
char* buf_ptr = buffer;
while (bytes_to_send > 0) {
bytes_sent = send(serverfd, buf_ptr, bytes_to_send, 0);
if (bytes_sent < 0) {
std::cerr << "Error sending file data";
fclose(fd);
return;
}
bytes_to_send -= bytes_sent;
buf_ptr += bytes_sent;
total_bytes += bytes_sent;
}
}
fclose(fd);
std::cout << "Image sent successfully! Total bytes: " << total_bytes;
}