-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcpconn.cpp
124 lines (110 loc) · 2.74 KB
/
tcpconn.cpp
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
#include "tcpconn.h"
int tcpConR(std::fstream& des, int port){
MyServerSocket sock;
sock.mbind(port);
sock.mlisten();
return 0;
}
int tcpConS(std::shared_ptr<MyServerSocket> sock, std::vector<char> des){
try {
sock->msend(des);
}catch(std::exception){
return 1;
}
return 0;
}
int tcpConSFileB(std::shared_ptr<MyServerSocket> sock, std::shared_ptr<std::fstream> file){
std::vector<char> data;
if(!file->is_open()){
return 1;
}
while(!file->eof()){
char symb;
file->read(&symb,1);
data.push_back(symb);
if(symb == 0xFF){
data.push_back(symb);
}
if(data.size() >= 2048){
try{
sock->msend(data);
data.clear();
} catch(std::exception& e){
return 1;
}
}
}
data.push_back(0xFF);
data.push_back(2);
try{
sock->msend(data);
} catch(std::exception& e){
return 1;
}
std::cout << "end file translation" << std::endl;
return 0;
}
int tcpConSFileA(std::shared_ptr<MyServerSocket> sock, std::shared_ptr<std::fstream> file){
std::vector<char> data;
if(!file->is_open()){
return 1;
}
while(!file->eof()){
char symb;
file->read(&symb,1);
data.push_back(symb);
if(data.size() >= 2048){
try{
sock->msend(data);
data.clear();
} catch(...){
return 1;
}
}
}
try{
sock->msend(data);
} catch(...){
return 1;
}
std::cout << "end file translation" << std::endl;
return 0;
}
int tcpConGFileA(std::shared_ptr<MyServerSocket> sock, std::shared_ptr<std::fstream> file){
if(!file->is_open()){
return 1;
}
int n = 1;
while(n){
std::vector<char> data = sock->mrecv(n);
for(unsigned int i =0 ; i < data.size(); i++){
file->write(&data[i],1);
}
}
std::cout << "end file translation" << std::endl;
return 0;
}
int tcpConGFileB(std::shared_ptr<MyServerSocket> sock, std::shared_ptr<std::fstream> file){
std::vector<char> data;
if(!file->is_open()){
return 1;
}
int n = 1;
while(n){
std::vector<char> data = sock->mrecv(n);
for(unsigned int i =0 ; i < data.size(); i++){
//cout << data[i];
if(data[i] == 0xFF){
if(data[i+1] == 0xFF){
i++;
} else {
n = 0;
break;
}
}
file->write(&data[i],1);
}
}
std::cout << "end file translation" << std::endl;
return 0;
}