feat(udp): Basic command parsing, bye command implemented
This commit is contained in:
		@@ -23,20 +23,36 @@ class UDPClient:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        client_socket.connect((self.server_name, self.server_port))
 | 
					        client_socket.connect((self.server_name, self.server_port))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        message = input("input lowercase sentence: ")
 | 
					        while True:
 | 
				
			||||||
 | 
					            try:
 | 
				
			||||||
 | 
					                # get command from user
 | 
				
			||||||
 | 
					                while (command := input(f"myftp> - {self.mode} - : ")) not in [
 | 
				
			||||||
 | 
					                    "put",
 | 
				
			||||||
 | 
					                    "get",
 | 
				
			||||||
 | 
					                    "summary",
 | 
				
			||||||
 | 
					                    "change",
 | 
				
			||||||
 | 
					                    "help",
 | 
				
			||||||
 | 
					                    "bye",
 | 
				
			||||||
 | 
					                ]:
 | 
				
			||||||
 | 
					                    print(
 | 
				
			||||||
 | 
					                        f"myftp> - {self.mode} - : Invalid command. Supported commands are put, get, summary, change and help"
 | 
				
			||||||
 | 
					                    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        client_socket.send(message.encode())
 | 
					                if command == "bye":
 | 
				
			||||||
 | 
					                    client_socket.close()
 | 
				
			||||||
 | 
					                    print(f"myftp> - {self.mode} - Session is terminated")
 | 
				
			||||||
 | 
					                    break
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        try:
 | 
					                client_socket.send(command.encode())
 | 
				
			||||||
            modified_message = client_socket.recv(2048)
 | 
					                modified_message = client_socket.recv(2048)
 | 
				
			||||||
            print(modified_message.decode())
 | 
					                print(modified_message.decode())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        except ConnectionRefusedError:
 | 
					            except ConnectionRefusedError:
 | 
				
			||||||
            print(
 | 
					                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."
 | 
					                    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."
 | 
				
			||||||
            )
 | 
					                )
 | 
				
			||||||
        finally:
 | 
					            finally:
 | 
				
			||||||
            client_socket.close()
 | 
					                client_socket.close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # ping pong UDP
 | 
					    # ping pong UDP
 | 
				
			||||||
    def check_udp_server(self):
 | 
					    def check_udp_server(self):
 | 
				
			||||||
@@ -55,7 +71,7 @@ class UDPClient:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            # If the server responds, consider the address valid
 | 
					            # If the server responds, consider the address valid
 | 
				
			||||||
            print(
 | 
					            print(
 | 
				
			||||||
                f"myftp> - {self.mode} - Server at {self.server_name} is valid. Response received: {data.decode('utf-8')}"
 | 
					                f"myftp> - {self.mode} - Server at {self.server_name}:{self.server_port} is valid. Response received: {data.decode('utf-8')}"
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # code reached here meaning no problem with the connection
 | 
					            # code reached here meaning no problem with the connection
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user