close socket if exception occurs too

This commit is contained in:
Christian Ulrich 2020-10-12 21:32:43 +02:00
parent fc9219ed80
commit 492e5ee72f
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
1 changed files with 5 additions and 3 deletions

View File

@ -23,6 +23,8 @@ proc getPrimaryIPAddr*(dest = parseIpAddress("8.8.8.8")): IpAddress =
newSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
else:
newSocket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)
try:
socket.connect($dest, 80.Port)
result = socket.getLocalAddr()[0].parseIpAddress()
finally:
socket.close()