当我在Ubuntu上运行server.py文件时,我正在使用while循环连续运行server.py以广播主机的IP地址,但是这导致发送了太多数据包,而我却无法可以访问Internet。由于许多数据包,整个服务器都处于阻塞状态。
我的问题是如何继续广播我的IP。
server.py
import socket
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST, 1)
sock.settimeout(0.2)
sock.bind(("", 44444))
ip_address=(socket.gethostbyname(socket.gethostname()))
while True:
sock.sendto(ip_address.encode('utf-8'),("255.255.255.255",12345))
client.py
import time
import socket
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
address = ("",12345)
sock.bind(address)
data,addr = sock.recvfrom(1024)
print (b"IP ADDRESS OF THE ROUTER:"+data)
print (addr)
time.sleep(500)