2011-12-12 33 views
2

我用PyQt4編寫的應用程序似乎沒有得到任何數據。 readyRead事件甚至沒有被觸發。 使用應用的是以下幾點:PyQt4的udp廣播似乎沒有工作

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) 

回答

1

在你的客戶,你是不是開始Qt的事件循環。您應該分配一個應用程序對象,並調用exec_並通過定時器處理debug_msg調用,或者使用QCoreApplication.processEvents()抽取事件循環。

另一種選擇是使用齊平():

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

bool QAbstractSocket::flush()

該函數將盡可能從內部寫 緩衝器向底層網絡插座,而不會阻塞。如果寫入任何數據 ,則此函數返回true;否則返回false。

如果您需要QAbstractSocket立即開始發送 緩衝數據,請調用此函數。成功寫入的字節數 取決於操作系統。在大多數情況下,您不需要調用此函數,因爲一旦控制權返回到事件循環,QAbstractSocket將自動開始發送數據 。如果沒有事件循環的 ,請改爲調用waitForBytesWritten()。

0

QtNetwork.QUdpSocket.NetworkError不是一個異常類型,而只是一個常量。而且,爲什麼要將套接字綁定到QHostAddress.Broadcast。你需要QHostAddress.Any嗎?