4

I have problem with QUdpSocket. I want to create a simple program to send and receive data using the UDP protokol. I already read many similar topic but I do not found solved. Communication worked only for QHostAdress::LocalHost, then I give this same data as send, but if I want to send data to outside set concrete address, for example 194.181.161.134, that does not worked. That mean data is send but I can't receive. This is my code:

class Okno_GL : public QMainWindow
{
    Q_OBJECT
public:
    explicit Okno_GL(QWidget *parent = 0);
    QWidget *wg;
    QPushButton *pb;
    QPushButton *pl;
    QGridLayout *gr;
    QUdpSocket *socket;
    QHostAddress host;
    QHostAddress bcast;


signals:

public slots:
    void SLOT_Write();
    void SLOT_load();        
};

class Receiver : public QObject
{
    Q_OBJECT
public:
    Receiver();

     QUdpSocket *udpSocket;
public slots:
     void SLOT_processPendingDatagrams();
     void SLOT_StCh(QAbstractSocket::SocketState state);
};

Okno_GL::Okno_GL(QWidget *parent) :
    QMainWindow(parent)
{
    pb = new QPushButton("write" , this);
    pl = new QPushButton("read" , this);
    wg = new QWidget(this);
    setCentralWidget(wg);
    gr = new QGridLayout(wg);
    gr->addWidget(pb);
    gr->addWidget(pl);
    socket = new QUdpSocket(this);

    connect(pb , SIGNAL(clicked()) , SLOT(SLOT_Write()));
    connect(pl , SIGNAL(clicked()) , SLOT(SLOT_load()));

}

void Okno_GL::SLOT_Write()
{
         QByteArray datagram = "gS";
         int send;
         send = socket->writeDatagram(datagram.data(),  QHostAddress("194.181.161.134"), 1200);       
}

void Okno_GL::SLOT_load()
{

}


Receiver::Receiver()
{


     udpSocket = new QUdpSocket(this);
     connect(udpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)) , this , SLOT(SLOT_StCh(QAbstractSocket::SocketState)));

     if(udpSocket->bind(QHostAddress::Any , 1200))
     {
         qd "bind";
     }
     else
     {
         qd "not bind";
     }

}

void Receiver::SLOT_processPendingDatagrams()
{
    qd "receiver";
    QByteArray datagram;

        do {
            datagram.resize(udpSocket->pendingDatagramSize());
            udpSocket->readDatagram(datagram.data(), datagram.size());
        } while (udpSocket->hasPendingDatagrams());

    qd "datagram" << datagram;
}

void Receiver::SLOT_StCh(QAbstractSocket::SocketState state)
{
    qd "slot" << state;

    QByteArray datagram = "gS";
    if ( state == QAbstractSocket::BoundState ) {

           connect(udpSocket, SIGNAL(readyRead()), this, SLOT(SLOT_processPendingDatagrams()) , Qt::QueuedConnection);
       }

}
msrd0
  • 7,816
  • 9
  • 47
  • 82
FanQt
  • 218
  • 1
  • 3
  • 9
  • is somewhere object of `Receiver` created ?? – Blueman Oct 20 '14 at 20:00
  • One important thing to verify: can you see the packet in Wireshark on the receiver machine? – jturcotte Oct 20 '14 at 20:03
  • Oh sorry for that. Ofcourse object Receiver is create in main this same as object Okno_GL. Ofocourse firs is createt Okno_GL. jtucotte ok I cheked this, and give reply – FanQt Oct 20 '14 at 20:37

2 Answers2

1

You should use write() instead of writeDatagram()

qint64 QUdpSocket::writeDatagram(const char * data, qint64 size, const QHostAddress & address, quint16 port)

Warning: Calling this function on a connected UDP socket may result in an error and no packet being sent. If you are using a connected socket, use write() to send datagrams.

Max Go
  • 2,092
  • 1
  • 16
  • 26
1

So, I rebuild my code accordingly with your proposition. I add this line in construktor class Okno_GL.

socket = new QUdpSocket(this);
socket->connectToHost(QHostAddress("194.181.161.134") , 1200);

Here is slot to send

void Okno_GL::SLOT_Write()
{
         QByteArray datagram = "gS";
         int send;
//         send = socket->writeDatagram(datagram.data(),  QHostAddress("194.181.161.134"), 1200);
         send = socket->write(datagram);
         qd " send" << send;

}

Receiver code is not change, so I still white for signal readRedy() ,and I do not get him after send data.

PS. I whant to add that I have other option to connetc, is very very simply file command *bat whitch connect this host without any problem . This is proof that my network or admin nothing blocks (port , ip);

Pss thanks for your interest

Problem solved ! It is very stupid mistake. I should be send 3 bajt instead 2. The 3 bajt it is 0x0D. Following code is work

 socket = new QUdpSocket(this);
 socket->bind(QHostAddress::Any, 1200);

and send

 QByteArray datagram = "gS";
 datagram.append(0x0D);
 int send;
 send = socket->writeDatagram(datagram.data(),  QHostAddress("194.181.161.134"), 1200);

and receive after signal readyRead()

qd "receiver";
        QByteArray datagram;

            do {
                datagram.resize(socket->pendingDatagramSize());
                socket->readDatagram(datagram.data(), datagram.size());
            } while (socket->hasPendingDatagrams());

        qd "datagram" << datagram;

very help mi wireshark. Thanks for all

FanQt
  • 218
  • 1
  • 3
  • 9