-
Notifications
You must be signed in to change notification settings - Fork 1
/
transmit.cpp
executable file
·77 lines (65 loc) · 2.55 KB
/
transmit.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
#include "transmit.h"
void transmit::accept(transmit& clt) {
if (!socket::socketAccept(clt)) {
throw SocketException("Could not accept socket");
}
cout << "connected" << endl;
}
void transmit::close() {
socket::close();
}
const transmit& transmit::operator << (const string &str) const {
if (!socket::send(str))
throw SocketException("Could not write to socket");
return *this;
}
const transmit& transmit::operator >> (string &str) const {
if (!socket::recv(str))
throw SocketException("Could not read from socket");
return *this;
}
void transmit::SendFile(const vector<string>& info) {
assert(info.size() == 3);
string pic = "temp/" + info[2].substr(info[2].find_last_of("/")+1);
string part = info[0] + "||||" + info[1] + "||||" + pic;
cout << part << endl;
if (!socket::send(part))
throw SocketException("Could not send to socket");
char buff[BUFFSIZE];bzero(buff,BUFFSIZE);
//read file
FILE *fd = fopen(info[2].c_str(),"rb");
if (fd == NULL) {
throw SocketException("Could not find a file");
} else {
cout << "file found" << endl;
bzero(buff,BUFFSIZE);
int file_block_length = 0;
while ((file_block_length = fread(buff,sizeof(char),BUFFSIZE,fd)) > 0) {
cout << "file_block_length:" << file_block_length << endl;
if(!socket::send(buff,file_block_length)) {
throw SocketException("Could not send file");
}
bzero(buff,BUFFSIZE);
}
fclose(fd);
printf("Transfer file finished !\n");
}
}
void transmit::SendStruct(string arg){
//struct MovieData movie;
//SetContent(movie, arg);
if(!socket::send((char*)arg.c_str(), BUFFSIZE)) {
throw SocketException("Could not send struct");
}
printf("Struct file finished !\n");
}
void transmit::SetContent(struct MovieData &movie, vector<string>& arg) {
memcpy(movie.name, arg[0].c_str(), arg[0].size()); cout << arg[0] << endl;
memcpy(movie.year, arg[1].c_str(), arg[1].size()); cout << arg[1] << endl;
memcpy(movie.length, arg[2].c_str(), arg[2].size()); cout << arg[2] << endl;
memcpy(movie.director, arg[3].c_str(), arg[3].size()); cout << arg[3] << endl;
memcpy(movie.cast, arg[4].c_str(), arg[4].size()); cout << arg[4] << endl;
memcpy(movie.pic,arg[6].c_str(), arg[6].size()); cout << arg[6] << endl;
memcpy(movie.number,arg[7].c_str(), arg[7].size()); cout << arg[7] << endl;
memcpy(movie.content, arg[5].c_str(), arg[5].size()); cout << arg[5] << endl;
}