Fixed and improved broadcast binding

This commit is contained in:
Artem30801
2019-03-20 11:37:33 +03:00
parent edbbfbbba6
commit 295ddb60f6
2 changed files with 22 additions and 17 deletions

View File

@@ -41,7 +41,7 @@ def get_ntp_time(host, port):
return unpacked[10] + float(unpacked[11]) / 2**32 - NTP_DELTA
def reconnect(t=2):
def reconnect(timeout=2, attempt_limit=10):
global clientSocket, host, port
print("Trying to connect to", host, ":", port, "...")
connected = False
@@ -50,29 +50,33 @@ def reconnect(t=2):
print("Waiting for connection, attempt", attempt_count)
try:
clientSocket = socket.socket()
# clientSocket.settimeout(3)
clientSocket.settimeout(timeout)
clientSocket.connect((host, port))
connected = True
print("Connection successful")
clientSocket.settimeout(None)
except socket.error as e:
print("Waiting for connection:", e)
time.sleep(t)
attempt_count +=1
if attempt_count >= 15:
print("Waiting for connection, can not connect:", e)
time.sleep(timeout)
attempt_count += 1
if attempt_count >= attempt_limit:
print("Too many attempts. Trying to get new server IP")
broadcst_client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
broadcst_client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
broadcst_client.bind(("", broadcast_port))
while True:
data, addr = broadcst_client.recvfrom(1024)
print("Recieved broadcast message %s from %s"%(data, addr))
if parse_command(data.decode("UTF-8"))[0] == "server_ip":
print("Binding to new IP: ", addr)
host, port = addr
print("Recieved broadcast message %s from %s" % (data, addr))
comm, args = parse_command(data.decode("UTF-8"))
print(comm, args)
if comm == "server_ip":
host, port = args[0], int(args[1])
print("Binding to new IP: ", host, port)
broadcst_client.close()
attempt_count = 0
break
def send_all(msg):
clientSocket.sendall(struct.pack('>I', len(msg)) + msg)

View File

@@ -24,12 +24,14 @@ import configparser
# All imports sorted in pyramid
# Functions
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
my_ip = s.getsockname()[0]
s.close()
return ip
return my_ip
def auto_connect():
@@ -46,15 +48,14 @@ def auto_connect():
def ip_broadcast(ip, port):
ip = ip
msg = bytes(Client.form_command("server_ip ", (ip, str(port))), "UTF-8")
broadcast_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
broadcast_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
broadcast_sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
while True:
msg = bytes(Client.form_command("server_ip ", ip, ), "UTF-8")
broadcast_sock.sendto(msg, ('255.255.255.255', broadcast_port))
print("Broadcast sent")
time.sleep(5)
time.sleep(10)
NTP_DELTA = 2208988800 # 1970-01-01 00:00:00
@@ -353,7 +354,7 @@ if __name__ == "__main__":
print('Server started on', host, ip, ":", port)
if USE_NTP:
now = time.ctime(get_ntp_time(NTP_HOST, NTP_PORT))
now = get_ntp_time(NTP_HOST, NTP_PORT)
else:
now = time.time()
print('Now:', time.ctime(now))