no message
parent
6d2b0c7309
commit
1a3840dca6
|
@ -37,10 +37,11 @@ NetworkController::NetworkController(NetworkController::NetWorkType type,
|
|||
}
|
||||
if((type == NetWorkType::TYPE_UDP_SERVER) || (type == NetWorkType::TYPE_TCP_CLIENT)){
|
||||
mUDP = new QUdpSocket(this);
|
||||
mUDP->bind(QHostAddress::LocalHost, port);
|
||||
mUDP->bind(QHostAddress::Any, port);
|
||||
mCnn = mUDP;
|
||||
connect(mUDP, SIGNAL(readyRead()),
|
||||
this, SLOT(on_ready_read()));
|
||||
mUDP->open(QIODevice::ReadWrite);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -86,7 +87,12 @@ NetworkController::~NetworkController()
|
|||
|
||||
void NetworkController::on_ready_read()
|
||||
{
|
||||
qDebug()<<QString::fromStdString(mCnn->readAll().toStdString());
|
||||
if((mType == TYPE_UDP_CLIENT )||(mType == TYPE_UDP_SERVER)){
|
||||
QNetworkDatagram datagram = mUDP->receiveDatagram();
|
||||
qDebug()<<datagram.senderAddress()<<datagram.senderPort()<<datagram.data();
|
||||
}else{
|
||||
qDebug()<<QString::fromStdString(mCnn->readAll().toStdString());
|
||||
}
|
||||
emit(on_data_recv());
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include <QTcpSocket>
|
||||
#include <QThread>
|
||||
#include <QTcpServer>
|
||||
#include <QNetworkDatagram>
|
||||
|
||||
// this is not a thread-safe class,any interface invoked in multi-thread maybe will cause unkown falut
|
||||
class NetworkController : public QObject
|
||||
|
|
|
@ -86,14 +86,15 @@ int ShareData::openNetwork(QString ip, unsigned int port, bool is_ws,int type)
|
|||
m_network_ = new NetworkController(NetworkController::TYPE_TCP_CLIENT,ip,port);
|
||||
return 0;
|
||||
}
|
||||
if(type == NetworkController::TYPE_UDP_CLIENT){
|
||||
|
||||
if((type == NetworkController::TYPE_UDP_CLIENT) ||
|
||||
(type == NetworkController::TYPE_UDP_SERVER)){
|
||||
m_network_ = new NetworkController((NetworkController::NetWorkType)type,ip,port);
|
||||
return 0;
|
||||
}
|
||||
if(type == NetworkController::TYPE_TCP_SERVER){
|
||||
m_network_ = new NetworkController(NetworkController::TYPE_TCP_SERVER,ip,port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue