A firewall will allow you to filter a network or areas of a network based on ports as well as the data inside of each packet.
Wikipedia
In computing, a firewall is a network security system that monitors and controls incoming and outgoing network traffic based on predetermined security rules. A firewall typically establishes a barrier between a trusted network and an untrusted network, such as the Internet.