扭曲的Python:UDP广播(简单的回声服务器)

扭曲的Python:UDP广播(简单的回声服务器),第1张

概述我正在尝试调整 Python Twisted – UDP examples以使用UDP广播.我可以从客户端发送消息并在服务器上接收它,但是,它不会发回消息. 客户: from twisted.internet.protocol import DatagramProtocolfrom twisted.internet import reactorfrom socket import SOL_S 我正在尝试调整 Python Twisted – UDP examples以使用UDP广播.我可以从客户端发送消息并在服务器上接收它,但是,它不会发回消息.

客户:

from twisted.internet.protocol import DatagramProtocolfrom twisted.internet import reactorfrom socket import Sol_SOCKET,SO_broADCASTclass EchoClIEntDatagramProtocol(DatagramProtocol):    strings = [        "Hello,world!","What a fine day it is.","Bye-bye!"    ]    def startProtocol(self):        self.transport.socket.setsockopt(Sol_SOCKET,SO_broADCAST,True)        self.transport.connect("255.255.255.255",8000)        self.sendDatagram()    def sendDatagram(self):        if len(self.strings):            datagram = self.strings.pop(0)            self.transport.write(datagram)        else:            reactor.stop()    def datagramReceived(self,datagram,host):        print 'Datagram received: ',repr(datagram)        self.sendDatagram()def main():    protocol = EchoClIEntDatagramProtocol()    #0 means any port    t = reactor.ListenUDP(0,protocol)    reactor.run()if __name__ == '__main__':   main()

服务器:

from twisted.internet.protocol import DatagramProtocolfrom twisted.internet import reactorclass EchoUDP(DatagramProtocol):    def datagramReceived(self,address):        print "Received from address: " + str(address)        print str(datagram)        self.transport.write(datagram,address)        print "Finished sending reply."print "Starting server."reactor.ListenUDP(8000,EchoUDP())reactor.run()

控制台输出:

Server:Starting server.Received from address ('192.168.1.137',53737)Hello,world!Finished sending reply.ClIEnt:no output.
解决方法 transport.connect创建一个 connected UDP socket

A connected UDP socket is slightly different from a standard one – it can only send and receive datagrams to/from a single address,but this does not in any way imply a connection. Datagrams may still arrive in any order,and the port on the other sIDe may have no one Listening. The benefit of the connected UDP socket is that it it may provIDe notification of undelivered packages. This depends on many factors,almost all of which are out of the control of the application,but it still presents certain benefits which occasionally make it useful.

我怀疑服务器的响应没有被客户端捕获,因为它正在侦听来自广播地址的响应,而不是服务器的特定地址.

相反,只需使用self.transport.write(数据,(主机,端口))形式的写入而不首先启动连接 – 这将允许客户端从任何地址接收数据包.

from twisted.internet.protocol import DatagramProtocolfrom twisted.internet import reactorfrom socket import Sol_SOCKET,True)        #self.transport.connect("255.255.255.255",8000) <- not needed        self.sendDatagram()    def sendDatagram(self):        if len(self.strings):            datagram = self.strings.pop(0)            self.transport.write(datagram,('255.255.255.255',8000)) # <- write to broadcast address here        else:            reactor.stop()    def datagramReceived(self,protocol)    reactor.run()if __name__ == '__main__':   main()
总结

以上是内存溢出为你收集整理的扭曲的Python:UDP广播(简单的回声服务器)全部内容,希望文章能够帮你解决扭曲的Python:UDP广播(简单的回声服务器)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1204973.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存