1 回答
TA贡献1875条经验 获得超5个赞
你有一些错误和非常难以理解的变量名称。
bind()
用于将服务器分配给本地网卡 - 而不是客户端 IP - 并且只使用一次 - 在循环之前不要关闭套接字,因为(我记得)它需要再次创建套接字
import socket
#server_ip = socket.gethostbyname(socket.gethostname()) # this gives me `127.0.1.1` because I have it in `/etc/hosts`
server_ip = '192.168.1.13' # <-- IP of my WiFi card on server
server_port = 4005
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#s.bind( (server_ip, server_port) ) # assign server to one local network card
s.bind( ('0.0.0.0', server_port) ) # assign server to all local network cards
text = f'{server_ip}:{server_port}'
print(text)
# --- loop ---
for i in range(1, 255):
client_ip = f'192.168.1.{i}'
client_port = 4005
print(f'{client_ip}:{client_port}')
s.sendto(text.encode('utf-8'), (client_ip, client_port))
# --- after loop ---
s.close() # only if you will no use this socket any more
添加回答
举报