Firewalls are used to control incoming and outgoing network traffic based on predetermined security rules, primarily by limiting traffic to only the necessary ports for an application. Port Filtering: Firewalls can block or allow traffic based on port numbers, thus limiting exposure to only those ports required by the application, enhancing security. Security: This helps to protect the application from unauthorized access and potential attacks by reducing the attack surface.