gpt4 book ai didi

python - PyQt 4's udp broadcast doesn' t 似乎可以工作

转载 作者:太空宇宙 更新时间:2023-11-04 06:25:38 24 4
gpt4 key购买 nike

我用 PyQt4 编写的应用程序似乎没有获取任何数据。readyRead 事件甚至没有被触发。应用程序的使用如下:

python server.py -s -p 50000 #(server on port 50000)
python server.py -c -p 50000 #(client sending data to port 50000)

//

import sys
import time

from PyQt4 import QtNetwork, QtCore
from optparse import OptionParser

class Server(object):

def __init__(self, port):

self.port = port

try:
self.socket = QtNetwork.QUdpSocket()
self.socket.bind(QtNetwork.QHostAddress.Broadcast, int(self.port), QtNetwork.QUdpSocket.ShareAddress)

self.socket.readyRead.connect(self.receiver)

except QtNetwork.QUdpSocket.NetworkError:
print "EXCEPTION DURING INITIALIZING SERVER'S SOCKET"
sys.exit(1)

def receiver(self):
print "DEBUG: RECEIVE"
while(self.socket.hasPendingDatagrams()):
try:
size = self.socket.pendingDatagramSize()
msg, host, port = self.socket.readDatagram(size)
except:
print "EXCEPTION DURING RECEIVEING AND READING DATAGRAM"
else:
print "HOST %s:%s MSG: %s" % (str(host), str(port), str(msg))

def __del__(self):
print "DESTRUCTOR"
self.socket.close()

class Client(object):

def __init__(self, port):

self.port = port

try:
self.socket = QtNetwork.QUdpSocket()
except:
print "EXCEPTION DURING INITIALIZING CLIENT'S SOCKET"

self.main_loop()

def main_loop(self):
for i in range(20):
self.debug_msg()
time.sleep(0.5)
print "EXITING"
self.socket.close()


def debug_msg(self):
msg = "DEBUG"
self.socket.writeDatagram(msg, QtNetwork.QHostAddress.Broadcast, int(self.port))


if __name__ == "__main__":

parser = OptionParser()
parser.add_option("-p", "", action="store", type="string", dest="port")
parser.add_option("-c", "", action="store_true", dest="client")
parser.add_option("-s", "", action="store_true", dest="server")

options, args = parser.parse_args()

if not (options.server or options.client):
print "Client/Server not specified. Could not continue..."
sys.exit(1)

elif not options.port:
print "Server's port not specified. Could not continue..."
sys.exit(1)

else:

if options.server:
serv = Server(options.port)

App = QtCore.QCoreApplication(sys.argv)
sys.exit(App.exec_())
else:
client = Client(options.port)

最佳答案

在您的客户端中,您没有启动 Qt 的事件循环。您应该分配一个应用程序对象并在其上调用 exec_ 并通过计时器处理 debug_msg 调用,或者您应该使用 QCoreApplication.processEvents() 泵送事件循环。

另一种选择是使用 flush():

http://doc.qt.nokia.com/latest/qabstractsocket.html#flush

bool QAbstractSocket::flush ()

This function writes as much as possible from the internal write buffer to the underlying network socket, without blocking. If any data was written, this function returns true; otherwise false is returned.

Call this function if you need QAbstractSocket to start sending buffered data immediately. The number of bytes successfully written depends on the operating system. In most cases, you do not need to call this function, because QAbstractSocket will start sending data automatically once control goes back to the event loop. In the absence of an event loop, call waitForBytesWritten() instead.

关于python - PyQt 4's udp broadcast doesn' t 似乎可以工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8475960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com