After finding a host on the network, perhaps after doing a ping sweep or monitoring the network traffic, you typically want to scan the ports and see which ports are open and accepting connections. You can learn a lot about a machine just by seeing what ports are open. You might be able to determine whether it is Windows or Linux or whether it is hosting an email server, a web server, a database server, and more.
There are many types of port scans, but this example demonstrates the most basic and straightforward port scan example, which is a TCP connect scan. It connects like any typical client and sees whether the server accepts the request. It does not send or receive any data and immediately disconnects, logging if it was successful.
The following example scans the localhost machine only and limits the ports checked to the reserved ports 0-1024. Database servers...