首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Udp广播节目

问Udp广播节目
EN

Stack Overflow用户
提问于 2019-12-04 11:12:17
回答 1查看 177关注 0票数 1

我在做一个UDP广播节目-

创建一个系统,将消息警报的广播发送到网络上的所有节点。每个接收消息的站必须将其重新发送到它所连接的网络上的其他站点(除外),以传播该消息。

我创建了client.py和server.py,但我不知道如何让电台重新发送消息警报。这是我第一次在python中创建网络编程项目。

代码语言:javascript
复制
#client.py
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP
# Enable port reusage so we will be able to run multiple clients and servers on single (host, port).
# Do not use socket.SO_REUSEADDR except you using linux(kernel<3.9): goto https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ for more information.
# For linux hosts all sockets that want to share the same address and port combination must belong to processes that share the same effective user ID!
# So, on linux(kernel>=3.9) you have to run multiple servers and clients under one user to share the same (host, port).
# Thanks to @stevenreddie
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
# Enable broadcasting mode
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
client.bind(("", 37020))
while True:
# Thanks @seym45 for a fix
data, addr = client.recvfrom(1024)
print("received message: %s"%data)


#server.py 
import socket
import time
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
# Enable port reusage so we will be able to run multiple clients and servers on single (host, port).
# Do not use socket.SO_REUSEADDR except you using linux(kernel<3.9): goto https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ for more information.
# For linux hosts all sockets that want to share the same address and port combination must belong to processes that share the same effective user ID!
# So, on linux(kernel>=3.9) you have to run multiple servers and clients under one user to share the same (host, port).
# Thanks to @stevenreddie
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
# Enable broadcasting mode
server.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# Set a timeout so the socket does not block
# indefinitely when trying to receive data.
server.settimeout(0.2)
message = b"your very important message"
while True:
server.sendto(message, ('<broadcast>', 37020))
print("message sent!")
time.sleep(1)
EN

回答 1

Stack Overflow用户

发布于 2019-12-04 14:02:40

您可以直接从sendto()从client.py调用

client2.py (这将重新发送接收到的消息)

代码语言:javascript
复制
import socket
import time
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
client.bind(("", 37020))
while True:
   data, addr = client.recvfrom(1024)
   print("received message: %s"%data)
   data = b"RESEND important message"
   client.sendto(data, ('<broadcast>', 37020))
   print("sent", data)
   time.sleep(1)

client.py (只接收)

代码语言:javascript
复制
import socket
import time
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
client.bind(("", 37020))
while True:
   data, addr = client.recvfrom(1024)
   print("received message: %s"%data)
   time.sleep(1)

server.py

代码语言:javascript
复制
import socket
import time
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
server.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
server.settimeout(0.2)
message = b"your very important message"
while True:
    server.sendto(message, ('<broadcast>', 37020))
    print("message sent!")
    time.sleep(1)

如果启动了所有三个文件,就会得到类似于server.py的输出。

代码语言:javascript
复制
message sent!
message sent!
message sent!
message sent!
message sent!
message sent!
message sent!

client2.py

代码语言:javascript
复制
received message: b'your very important message'
sent b'RESEND important message'
received message: b'RESEND important message'
sent b'RESEND important message'
received message: b'your very important message'
sent b'RESEND important message'
received message: b'RESEND important message'
sent b'RESEND important message'
received message: b'your very important message'
sent b'RESEND important message'
received message: b'RESEND important message'
sent b'RESEND important message'

client.py

代码语言:javascript
复制
received message: b'your very important message'
received message: b'your very important message'
received message: b'RESEND important message'
received message: b'your very important message'
received message: b'RESEND important message'
received message: b'your very important message'
received message: b'RESEND important message'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59174673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档