gpt4 book ai didi

python - 扭曲的 Python : UDP Broadcast (simple echo server)

转载 作者:太空狗 更新时间:2023-10-30 01:22:31 25 4
gpt4 key购买 nike

我正在尝试调整 Python Twisted - UDP examples使用UDP广播。我可以从客户端发送消息并在服务器上接收它,但是,它不会发回消息。

客户:

from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor

from socket import SOL_SOCKET, SO_BROADCAST

class 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 DatagramProtocol
from twisted.internet import reactor

class EchoUDP(DatagramProtocol):
def datagramReceived(self, datagram, 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(data, (host, port)) 形式的写入而不首先启动连接 - 这将允许客户端从任何地址接收数据包。

from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor

from socket import SOL_SOCKET, SO_BROADCAST

class 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) <- 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, 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()

关于python - 扭曲的 Python : UDP Broadcast (simple echo server),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21770219/

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