fraubrj
27th December 2008, 10:07
I have two programs, A and B, who communicate by an UDP connection. They can beings on the same computer or two computers on a network.
The A program sends a command to the B program, which processes this command and sends then an answer to the A program.
Originally both programs were written in C# (WinXP). The A program was rewritten with Qt/C++, and it works well with the C# version of B (WinXP).
But I don't manage to put on the UDP connection with a Qt/C++ version of B.
Part of the A program
class radioInterfaceThread : public QThread
{
Q_OBJECT
QUdpSocket *udpCommandSocket;
public:
radioInterfaceThread(QObject *parent = 0);
~radioInterfaceThread();
void setConnectUDP();
protected:
void run();
private slots:
void udpRxEvent();
void sendCommand();
};
radioInterfaceThread::radioInterfaceThread(QObject *parent)
: QThread(parent)
, udpCommandSocket(0)
{
udpCommandSocket = new QUdpSocket;
connect(udpCommandSocket, SIGNAL(readyRead()), this, SLOT(udpRxEvent()), Qt::DirectConnection);
}
void radioInterfaceThread::run()
{
....
exec();
....
}
void radioInterfaceThread::setConnectTCP(QString addr, quint16 port)
{
udpCommandSocket->connectToHost(QHostAddress(addr), port, QAbstractSocket::ReadWrite);
}
void radioInterfaceThread::udpRxEvent()
{
qint64 bytes_available = 0;
while ((bytes_available = udpCommandSocket->bytesAvailable()) > 0)
{
QByteArray datagram;
datagram.resize(bytes_available);
qint64 bytes_read = udpCommandSocket->readDatagram(datagram.data(), datagram.size());
//... process the answer
}
}
// slot called to send Command
void radioInterfaceThread::sendCommand(QString cmd)
{
qint64 bytes_sent = 0;
//...
bytes_sent = udpCommandSocket->write(QString(cmd.trimmed() + "\r\n").toLatin1().constData());
//...
}
The A program sends a command to the B program, which processes this command and sends then an answer to the A program.
Originally both programs were written in C# (WinXP). The A program was rewritten with Qt/C++, and it works well with the C# version of B (WinXP).
But I don't manage to put on the UDP connection with a Qt/C++ version of B.
Part of the A program
class radioInterfaceThread : public QThread
{
Q_OBJECT
QUdpSocket *udpCommandSocket;
public:
radioInterfaceThread(QObject *parent = 0);
~radioInterfaceThread();
void setConnectUDP();
protected:
void run();
private slots:
void udpRxEvent();
void sendCommand();
};
radioInterfaceThread::radioInterfaceThread(QObject *parent)
: QThread(parent)
, udpCommandSocket(0)
{
udpCommandSocket = new QUdpSocket;
connect(udpCommandSocket, SIGNAL(readyRead()), this, SLOT(udpRxEvent()), Qt::DirectConnection);
}
void radioInterfaceThread::run()
{
....
exec();
....
}
void radioInterfaceThread::setConnectTCP(QString addr, quint16 port)
{
udpCommandSocket->connectToHost(QHostAddress(addr), port, QAbstractSocket::ReadWrite);
}
void radioInterfaceThread::udpRxEvent()
{
qint64 bytes_available = 0;
while ((bytes_available = udpCommandSocket->bytesAvailable()) > 0)
{
QByteArray datagram;
datagram.resize(bytes_available);
qint64 bytes_read = udpCommandSocket->readDatagram(datagram.data(), datagram.size());
//... process the answer
}
}
// slot called to send Command
void radioInterfaceThread::sendCommand(QString cmd)
{
qint64 bytes_sent = 0;
//...
bytes_sent = udpCommandSocket->write(QString(cmd.trimmed() + "\r\n").toLatin1().constData());
//...
}