【PyQt4 实例19】基于UDP的网络广播程序

udpserver:

# -*- coding: utf-8 -*-
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
from PyQt4.QtNetwork import *

QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))

class Udpserver(QDialog):
    def __init__(self,parent=None):
        super(Udpserver,self).__init__(parent)
        self.setWindowTitle(self.tr("UDP Server"))
        vbMain = QVBoxLayout(self)
        
        LableTimer=QLabel(self.tr("Timer:"))
        vbMain.addWidget(LableTimer)
        self.LineEditText=QLineEdit()
        vbMain.addWidget(self.LineEditText)
        self.PushButtonStart = QPushButton(self.tr("Start"))
        vbMain.addWidget(self.PushButtonStart)
        self.connect(self.PushButtonStart,SIGNAL("clicked()"),self.slotButton)
        self.port = 5555
        self.isStarted = False
        self.udpSocket = QUdpSocket(self)
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.slotTimeout)
    
    def slotTimeout(self):
        msg = self.LineEditText.text()
        length = 0
        if msg == "" :
            return
        length = self.udpSocket.writeDatagram(msg.toLatin1(),QHostAddress.Broadcast,self.port)
        if length != msg.length():
            return
        
    def slotButton(self):
        if self.isStarted == False: 
            self.isStarted = True      
            self.PushButtonStart.setText("Stop")
            self.timer.start(1000) 
        else:
            self.isStarted = False
            self.PushButtonStart.setText("Start")
            self.timer.stop()
        
app=QApplication(sys.argv)
dialog=Udpserver()
dialog.show()
app.exec_()

udpClient:

# -*- coding: utf-8 -*-
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
from PyQt4.QtNetwork import *

QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))

class UdpClient(QDialog):
    def __init__(self,parent=None):
        super(UdpClient,self).__init__(parent)
        self.setWindowTitle(self.tr("UDP Client"))
        vbMain = QVBoxLayout(self)
        
        self.LineEditReceive=QTextEdit()
        vbMain.addWidget(self.LineEditReceive)
        self.PushButtonClose = QPushButton(self.tr("Close"))
        vbMain.addWidget(self.PushButtonClose)
        self.connect(self.PushButtonClose,SIGNAL("clicked()"),self.slotButton)
        self.port = 5555
        self.udpSocket = QUdpSocket(self)
        self.connect(self.udpSocket,SIGNAL("readyRead()"),self.dataReceive)
        
        result = self.udpSocket.bind(self.port)
        if not result:
            QMessageBox.information(self,self.tr("error"),self.tr("udpserver create error!"))
            return
    
    def dataReceive(self):
        while self.udpSocket.hasPendingDatagrams():

            msglist = self.udpSocket.readDatagram(self.port)
            msg = msglist[0]
            self.LineEditReceive.insertPlainText(msg)
        
    def slotButton(self):
        pass
        
app=QApplication(sys.argv)
dialog=UdpClient()
dialog.show()
app.exec_()



你可能感兴趣的:(python)