-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtcp_file_server.cpp
More file actions
99 lines (88 loc) · 2.77 KB
/
tcp_file_server.cpp
File metadata and controls
99 lines (88 loc) · 2.77 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
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
#include "tcp_file_server.h"
tcp_file_server::tcp_file_server(quint16 port, QObject *parent) : QTcpServer(parent)
{
bind_port = port;
}
tcp_test_client::tcp_test_client(quint16 port, QObject *parent) : QTcpSocket(parent)
{
bind_port = port;
}
void tcp_file_server::start_service(){
if(!listen(QHostAddress::LocalHost, bind_port)){
qDebug() << "can't bind port " << bind_port ;
QThread::sleep(5);
qApp->exit(3);
return;
}
qDebug() << "FTP Service Started";
connect(this,SIGNAL(newConnection()),this,SLOT(newClient()));
}
void tcp_file_server::newClient()
{
QTcpSocket *tcp_client = nextPendingConnection();
if(!tcp_client->waitForReadyRead(1000)){
qDebug() << "Client timed out!";
return;
}
buffer=tcp_client->readAll();
if( buffer.contains(ftp_header) ){
buffer = QString("ACK").toUtf8();
tcp_client->write(buffer);
QString file_name;
QFile local_file;
if(tcp_client->waitForReadyRead(1000)){
buffer=tcp_client->readAll();
file_name=QString::fromUtf8(buffer.data());
//local_file.setFileName(file_name);
local_file.setFileName("temp.out");
local_file.open(QIODevice::WriteOnly);
while(tcp_client->waitForReadyRead(100)){
buffer=tcp_client->readAll();
local_file.write(buffer);
}
local_file.close();
qDebug()<<"File Recieved";
}
}
}
void tcp_handle::parse_packet(){
}
void tcp_handle::recieve_file(){
}
void tcp_test_client::test_message(){
connectToHost(QHostAddress::LocalHost, bind_port);
if(!waitForConnected(1000)) {
qDebug() << "Could Not Connect!";
QThread::sleep(5);
qApp->exit(3);
return;
}
//buffer = QString("Testing Self").toUtf8();
//write(buffer);
send_file("dream.jpg");
}
void tcp_test_client::send_file(QString file_name){
QFile local_file(file_name);
if(!local_file.open(QIODevice::ReadOnly)){ qDebug() << "File not available"; return;}
if(!isWritable()){
qDebug() << "Not writable";
return;
}
buffer = QString(ftp_header).toUtf8();
qDebug() << "Sending " << buffer;
write(buffer);
if(!waitForReadyRead(1000)) {
qDebug() << "No Response";
return;
}
buffer = readAll();
if(!buffer.contains("ACK") ) { qDebug() << "Invalid Repsonse" << buffer.data(); return;}
buffer = file_name.toUtf8();
write(buffer);
waitForBytesWritten();
while(!local_file.atEnd()){
write(local_file.read(tcp_max_size));
}
local_file.close();
buffer = QString(eof_header).toUtf8();
}