Muhammad Essam
Muhammad Essam

Reputation: 57

PyQt5 QUdpSocket not binding to address and a port

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

Upvotes: 0

Views: 1336

Answers (1)

S. Nick
S. Nick

Reputation: 13661

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

Upvotes: 1

Related Questions