-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleFile.cpp
More file actions
47 lines (42 loc) · 1.01 KB
/
SimpleFile.cpp
File metadata and controls
47 lines (42 loc) · 1.01 KB
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
#include "SimpleFile.h"
#include <iostream>
using namespace std;
SimpleFile::SimpleFile(int client, string fileName) :client(client) {
int ret = fopen_s(&resource, fileName.c_str(), "rb");
if (ret < 0) { // if file not found, send a not-found page to browser
NotFound();
}
else { // if found, send the file to browser
SendFile();
}
}
/*
SimpleFile::~SimpleFile() {
fclose(resource);
}*/
void SimpleFile::SendFile() {
char buff[4096]; // a temp data package
int count = 0; // length of sent data
while (1) { // a cycle until all the resource is sent to buff and sent to the browser
// read data from resource to buff
int ret = fread(buff, sizeof(char), sizeof(buff), resource);
if (ret <= 0) {
break;
}
count += ret;
// send buff
int sent = send(client, buff, ret, 0);
if (sent == -1) {
perror("send");
}
else {
printf("sent %d bytes \n", sent);
}
}
printf("Ò»¹²·¢ËÍ%d×Ö½Ú\n", count);
fclose(resource);
}
// a todo function
void SimpleFile::NotFound() {
cout << "to do" << endl;
}