-
Notifications
You must be signed in to change notification settings - Fork 3
/
tcpserver.cpp
53 lines (46 loc) · 1.24 KB
/
tcpserver.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
#include "tcpserver.h"
TcpServer::TcpServer(QObject *parent) : BaseParent(parent)
{
server = new QTcpServer(this);
connect(server,&QTcpServer::newConnection,this,&TcpServer::handleNewConnection);
}
bool TcpServer::Open(Para *para)
{
return server->listen(QHostAddress::AnyIPv4,para->server_port);
}
void TcpServer::Close()
{
if(socket) {
socket->disconnectFromHost();
socket->deleteLater();
socket = NULL;
}
server->close();
}
//void TcpServer::incomingConnection(qintptr handle)
//{
// if (!socket) {
// socket = new QTcpSocket(this);
// } else {
// disconnect(socket,&QTcpSocket::readyRead,this,&TcpServer::handleReadyRead);
// socket->disconnectFromHost();
// }
// socket->setSocketDescriptor(handle);
// connect(socket,&QTcpSocket::readyRead,this,&TcpServer::handleReadyRead);
//}
void TcpServer::handleReadyRead()
{
emit sig_recv_data(socket->readAll());
}
void TcpServer::handleNewConnection()
{
socket = server->nextPendingConnection();
connect(socket,&QTcpSocket::readyRead,this,&TcpServer::handleReadyRead);
}
void TcpServer::slot_send_data(QByteArray hex)
{
if(socket && socket->isWritable()) {
socket->write(hex);
socket->flush();
}
}