#include "UDP.h"
{
}
//=======================================
void UDP::UDPInit(int port)
{
socketPort = port;
myPort = 11000;
bool res
= udpsocket
->bind
(QHostAddress("192.168.100.143"),myPort
);
connect(udpsocket,SIGNAL(connected()),this,SLOT(socketConnected()));
connect(udpsocket, SIGNAL(readyRead()), this, SLOT(readSocket()));
}
//=======================================
{
qDebug() << "UDPCLIENT ERROR: "<< udpsocket->errorString();
}
//========================================
void UDP::socketConnected()
{
qDebug() << "UDPCLIENT : Socket connected!";
emit clientLogMessage
(QString("UDPCLIENT : Connected !"));
}
{
if(udpsocket
->writeDatagram
(data.
data(),
QHostAddress(ip
),socketPort
)==-1) emit clientLogMessage
(QString("UDPCLIENT : Write problem !"));
else
udpsocket->flush();
}
void UDP::readSocket()
{
while (udpsocket->hasPendingDatagrams())
{
datagram.resize(udpsocket->pendingDatagramSize());
qDebug() << udpsocket->pendingDatagramSize();
udpsocket->readDatagram(datagram.data(), datagram.size(), host, &myPort);
emit dataReceived(datagram);
qDebug() << datagram.toHex();
}
}
#include "UDP.h"
UDP::UDP(QObject *parent) :
QObject(parent)
{
}
//=======================================
void UDP::UDPInit(int port)
{
socketPort = port;
host = new QHostAddress("192.168.100.143");
myPort = 11000;
udpsocket = new QUdpSocket(this);
bool res = udpsocket->bind(QHostAddress("192.168.100.143"),myPort);
connect(udpsocket,SIGNAL(connected()),this,SLOT(socketConnected()));
connect(udpsocket, SIGNAL(readyRead()), this, SLOT(readSocket()));
}
//=======================================
void UDP::socketError( QAbstractSocket::SocketError )
{
qDebug() << "UDPCLIENT ERROR: "<< udpsocket->errorString();
}
//========================================
void UDP::socketConnected()
{
qDebug() << "UDPCLIENT : Socket connected!";
emit clientLogMessage(QString("UDPCLIENT : Connected !"));
}
void UDP::sendCommand(QByteArray data)
{
if(udpsocket->writeDatagram(data.data(),QHostAddress(ip),socketPort)==-1)
emit clientLogMessage(QString("UDPCLIENT : Write problem !"));
else
udpsocket->flush();
}
void UDP::readSocket()
{
while (udpsocket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(udpsocket->pendingDatagramSize());
qDebug() << udpsocket->pendingDatagramSize();
udpsocket->readDatagram(datagram.data(), datagram.size(), host, &myPort);
emit dataReceived(datagram);
qDebug() << datagram.toHex();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks