[Python] how could a server send data to a client behind nat by udp in qt?

Discussão em 'Python' iniciado por Stack, Janeiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    The server has a public IP, the client is behind a nat.

    How could the client communicate with server by udp in qt?

    The client will send some data to server first, then how could server reply to client?

    The current code is this:

    server:

    self.udpSocketGet = QtNetwork.QUdpSocket()
    self.udpSocketGet.bind(QtNetwork.QHostAddress.LocalHost, serverPort)
    self.udpSocketGet.readyRead.connect(self.receive)

    def receive(self):
    while self.udpSocketGet.hasPendingDatagrams():
    size = self.udpSocketGet.pendingDatagramSize()
    if size > 0:
    data, senderAddr, senderPort = self.udpSocketGet.readDatagram(size)


    client:

    def sentToServer(self,data):
    udpSocketSend = QtNetwork.QUdpSocket()
    udpSocketSend.writeDatagram(data.encode('utf-8'), serverAddress, serverPort)

    Continue reading...

Compartilhe esta Página