-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfileserver.cpp
50 lines (44 loc) · 1.15 KB
/
fileserver.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
#include "fileserver.h"
const quint16 TCPPORT = 9633;
fileserver::fileserver(QObject *parent) : QObject(parent),\
conneted_num(0)
{
QObject::connect(&this->tcpServer,&QTcpServer::newConnection,this,&fileserver::newclientconneted);
}
void fileserver::listen(QHostAddress serveraddress)
{
if(!tcpServer.isListening())
tcpServer.listen(serveraddress,TCPPORT);
}
void fileserver::sendfile(QString filename,qint32 clientNum)
{
this->localFile = new QFile(filename);
if(!localFile->open(QFile::ReadOnly))
{
qDebug() << "open file error!";
return;
}
this->clientNum_ = clientNum;
}
void fileserver::newclientconneted()
{
QTcpSocket *peersocket = this->tcpServer.nextPendingConnection();
QHostAddress peeraddr = peersocket->peerAddress();
tcpclient *client = new tcpclient(this->localFile,peersocket,peeraddr);
emit getconneted(client);
clients_.push_back(client);
conneted_num++;
}
void fileserver::clear()
{
qDeleteAll(clients_.begin(),clients_.end());
clients_.clear();
}
void fileserver::fininshone()
{
conneted_num--;
if(conneted_num==0)
{
localFile->close();
}
}