In my program, I send a sequence of bytes to the device ( 68, 1, 0 ). The device responds with a sequence of bytes ( 68, 1, 0 ). All this I see in the program WireShark. But I can not accept the package because bind does not occur with the address 192.168.0.255:1104 . Who can tell how to do this?
P.S. Already clambered all QT assistant, but did not find the answer.
UPDATE new program code (this is a test code, just to deal with working with sockets):

 #include "UDP.h" UDP_Manager::UDP_Manager() { QHostAddress address("192.168.0.104"); Udp_Socket = new QUdpSocket(this); bool Is_Bound = Udp_Socket->bind(QHostAddress::Any, 1104); QByteArray Data; Data.resize(3); Data[0] = 68; Data[1] = 1; Data[2] = 0; Udp_Socket->writeDatagram(Data, address, 1004); _sleep(500); char Buf[4]; qint64 i = Udp_Socket->readDatagram(Buf, sizeof(Buf)); } 

Screen from WireShark program:
enter image description here

    1 answer 1

    Receiving broadcast does not differ from receiving ordinary messages, you just need to 'bind' to the local port and address.

    Errors in the code above:

    First, two sockets are not necessary here, one is enough (if in the future they will not hang some different logic). A socket is a duplex abstraction.

    Secondly, bind() always on the local address (192.168.0.11 in this case), if you need to listen to all local interfaces, you should bind to 0.0.0.0 (aka QHostAddress::Any ).

    Third, checking the Udp_Socket_RX->state() newly created socket is pointless.

    As a result, the entire initialization collapses:

     QHostAddress address("192.168.0.104"); Udp_Socket = new QUdpSocket(this); Udp_Socket->bind(QHostAddress::Any, 1104); connect(Udp_Socket_RX, SIGNAL(readyRead()), this, SLOT(Receive())); QByteArray Data {68,1,0}; Udp_Socket->writeDatagram(Data, address, 1004); 

    Fourthly, when reading from a socket, Address_RX and Port are output parameters, they should be checked after reading, and not set before.

    Otherwise, everything seems to be working ...

    • PS: look in wireshark that the package is really Broadcast, and not just with a strange address ... - Fat-Zer
    • changed the code, bind works, but there is still no readyRead () signal - George Tuzikov
    • I took out the reception from the slot, I accept the answer, but not always. 50/50. How to fix? - George Tuzikov
    • @GeorgeTuzikov, do they register in wireshark every time? otherwise I know one joke about UDP ... yes, and the original version of the source code is better to return to the question, but to add a new version below Update'om ... otherwise the answer is not readable ... - Fat-Zer
    • are recorded every time. I will return the code now - George Tuzikov