PyQt5 QUdpSocket 未绑定到地址和端口
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()
这不起作用套接字未绑定到端口我不知道为什么
我需要让服务器等待消息但不使用信号
试一试:
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()
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()
这不起作用套接字未绑定到端口我不知道为什么 我需要让服务器等待消息但不使用信号
试一试:
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()