Skip to content

Commit

Permalink
udp server on the command line now working
Browse files Browse the repository at this point in the history
  • Loading branch information
dannagle committed May 27, 2024
1 parent 5cb6bfa commit 3d2fa4e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
20 changes: 1 addition & 19 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1129,25 +1129,7 @@ int main(int argc, char *argv[])

if (sock.hasPendingDatagrams()) {


QHostAddress sender;
int senderPort;

QNetworkDatagram theDatagram = sock.receiveDatagram(10000000);
QByteArray recvData = theDatagram.data();
sender = theDatagram.senderAddress();
senderPort = theDatagram.senderPort();

QString hexString = Packet::byteArrayToHex(recvData);
if (quiet) {
out << "\n" << hexString;
} else {
out << "\nFrom: " << sender.toString() << ", Port:" << senderPort;
out << "\nResponse Time:" << QDateTime::currentDateTime().toString(DATETIMEFORMAT);
out << "\nResponse HEX:" << hexString;
out << "\nResponse ASCII:" << Packet::hexToASCII(hexString);
}

out << MainPacketReceiver::datagramOutput(sock.receiveDatagram(10000000), quiet);
out.flush();
}
}
Expand Down
37 changes: 28 additions & 9 deletions src/mainpacketreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,18 @@ void MainPacketReceiver::readPendingDatagrams()
{
while (udpSocket->hasPendingDatagrams()) {

QHostAddress sender;
int senderPort;
QString output = MainPacketReceiver::datagramOutput(udpSocket->receiveDatagram(10000000), false);

QNetworkDatagram theDatagram = udpSocket->receiveDatagram(10000000);
QByteArray datagram = theDatagram.data();
sender = theDatagram.senderAddress();
senderPort = theDatagram.senderPort();

QDEBUG() << "data size is" << datagram.size() << sender << senderPort;
QTextStream out(stdout);
out << output << Qt::endl;
out.flush();

}

}

bool MainPacketReceiver::initUDP(QString host, int port)
{

udpSocket = new QUdpSocket(this);
QHostAddress addy(host);
udpSocket->bind(addy, port);
Expand All @@ -83,6 +78,30 @@ bool MainPacketReceiver::initSSL(QString host, int port)
return tcpServer->isListening();
}

QString MainPacketReceiver::datagramOutput(QNetworkDatagram theDatagram, bool quiet)
{
QString output;
QTextStream out(&output);

QHostAddress sender;
int senderPort;

QByteArray recvData = theDatagram.data();
sender = theDatagram.senderAddress();
senderPort = theDatagram.senderPort();

QString hexString = Packet::byteArrayToHex(recvData);
if (quiet) {
out << "\n" << hexString;
} else {
out << "\nFrom: " << sender.toString() << ", Port:" << senderPort;
out << "\nResponse Time:" << QDateTime::currentDateTime().toString(DATETIMEFORMAT);
out << "\nResponse HEX:" << hexString;
out << "\nResponse ASCII:" << Packet::hexToASCII(hexString);
}

return output;
}


void MainPacketReceiver::send(Packet packetToSend) {
Expand Down
2 changes: 2 additions & 0 deletions src/mainpacketreceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ class MainPacketReceiver : public QObject
bool initUDP(QString host, int port);
bool initSSL(QString host, int port);

static QString datagramOutput(QNetworkDatagram theDatagram, bool quiet = false);
signals:
void sendPacket(Packet packetToSend);
//void receivedPacket(Packet packetReceived);


private:
Expand Down

0 comments on commit 3d2fa4e

Please sign in to comment.