fix: open/close client sockets
This commit is contained in:
parent
d369b8e1b1
commit
253ccdf1ff
@ -52,11 +52,10 @@ class UDPClient:
|
|||||||
if not self.pong_received:
|
if not self.pong_received:
|
||||||
return
|
return
|
||||||
|
|
||||||
while True:
|
client_socket = socket(AF_INET, SOCK_DGRAM)
|
||||||
try:
|
|
||||||
client_socket = socket(AF_INET, SOCK_DGRAM)
|
|
||||||
client_socket.connect((self.server_name, self.server_port))
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
# get command from user
|
# get command from user
|
||||||
command = input(f"myftp> - {self.mode} - : ").strip().lower()
|
command = input(f"myftp> - {self.mode} - : ").strip().lower()
|
||||||
|
|
||||||
@ -112,19 +111,22 @@ class UDPClient:
|
|||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
client_socket.send(request_payload.encode("utf-8"))
|
client_socket.sendto(request_payload.encode("utf-8"), (self.server_name, self.server_port))
|
||||||
modified_message = client_socket.recv(2048)[1:]
|
|
||||||
print(modified_message.decode())
|
|
||||||
client_socket.close() # type: ignore
|
|
||||||
|
|
||||||
except ConnectionRefusedError:
|
modified_message = client_socket.recv(2048)[1:]
|
||||||
print(
|
|
||||||
f"myftp> - {self.mode} - ConnectionRefusedError happened. Please restart the client program, make sure the server is running and/or put a different server name and server port."
|
print(modified_message.decode())
|
||||||
)
|
|
||||||
except Exception as error:
|
except ConnectionRefusedError:
|
||||||
print(
|
print(
|
||||||
f"myftp> - {self.mode} - {error} happened."
|
f"myftp> - {self.mode} - ConnectionRefusedError happened. Please restart the client program, make sure the server is running and/or put a different server name and server port."
|
||||||
)
|
)
|
||||||
|
except Exception as error:
|
||||||
|
print(
|
||||||
|
f"myftp> - {self.mode} - {error} happened."
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
client_socket.close()
|
||||||
|
|
||||||
# ping pong UDP
|
# ping pong UDP
|
||||||
def check_udp_server(self):
|
def check_udp_server(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user