0
def main():
    app = QCoreApplication([])
    local_ip = QHostAddress('192.168.43.126')
    port = 2359
    udp_socket = QUdpSocket()
    b = QByteArray()
    b.append('i0000,0000')
    udp_socket.writeDatagram(b, QHostAddress('192.168.43.1'), port)
    udp_socket.bind(QHostAddress('192.168.43.126'),port)
    while True:
        #print('in the while loop')
        while udp_socket.hasPendingDatagrams():
            #print('has pending datadrams ')
            s = udp_socket.readDatagram(10)
            print(s)

    app.exec_()


if __name__ == '__main__':
    main()

this i snot working the Socket is not binding to the port i don't knew why i need to make the server waits for a message but not using signals

1 Answers1

1

Try it:

import sys
from PyQt5.QtGui     import *
from PyQt5.QtCore    import *
from PyQt5.QtWidgets import *
from PyQt5.QtNetwork import QUdpSocket, QHostAddress

def main():
    app = QCoreApplication([])
    local_ip = QHostAddress('192.168.43.126')
    port = 2359
    udp_socket = QUdpSocket()
    b = QByteArray()
    b.append('i0000,0000')

    #udp_socket.bind(QHostAddress('192.168.43.126'),port)
    udp_socket.bind(QHostAddress.LocalHost, port)      

    #udp_socket.writeDatagram(b, QHostAddress('192.168.43.1'), port)
    udp_socket.writeDatagram(b, QHostAddress.LocalHost, port)

    while True:
        #print('in the while loop')
        while udp_socket.hasPendingDatagrams():
            #print('has pending datadrams ')
            s = udp_socket.readDatagram(10)
            print("\nQHostAddress.LocalHost", QHostAddress(QHostAddress.LocalHost).toString())
            print(s)

    app.exec_()


if __name__ == '__main__':
    main()

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33
  • the problem is the following when i bind to my computer (works as a server) it isn't binding udp.bind(ip, port) returns false. i need to allow some other device send UDP message to my computer @S .Nick – Muhammad Essam Jun 22 '18 at 13:07