Building a simple TCP port scanner in C

Building A Simple TCP Port Scanner in C

Overview

This project demonstrates how to build a basic TCP port scanner in C. Port scanning is a technique used to determine which ports on a target host are open. This knowledge is essenti…


This content originally appeared on DEV Community and was authored by Jonathan Peters

Building A Simple TCP Port Scanner in C

Overview

This project demonstrates how to build a basic TCP port scanner in C. Port scanning is a technique used to determine which ports on a target host are open. This knowledge is essential for network administration and security.

Features

  • Scans ports 1–1024 (well-known ports) on the target (default: localhost)
  • Reports open ports to the console
  • Easy to extend for remote targets and custom port ranges

How It Works

  • Uses TCP sockets to attempt connections to each port.
  • If a connection succeeds, the port is open.
  • Each port is scanned sequentially.

Getting Started

Prerequisites

  • GCC or any C compiler
  • Unix-like OS (Linux, macOS)

Build

gcc -o port_scanner port_scanner.c

Run

./port_scanner

Code Explanation

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

int main() {
    int sockfd;
    struct sockaddr_in serv_addr;

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Scan localhost

    for (int port = 1; port <= 1024; port++) {
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0) {
            perror("Socket creation failed");
            continue;
        }
        serv_addr.sin_port = htons(port);

        if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == 0) {
            printf("Port %d is open\n", port);
        }
        close(sockfd);
    }
    return 0;
}
  1. Set up the target address for scanning (127.0.0.1).
  2. Iterate over the desired port range.
  3. Create a new socket for each port.
  4. Attempt to connect to each port.
  5. Print open ports.
  6. Close the socket to avoid resource leaks.

Tips and Tricks

  • Use multithreading for faster scanning.
  • Set timeouts to avoid hanging on closed ports.
  • Accept IP/port range as input for flexibility.

Extensions

  • Scan remote hosts by changing the IP address.
  • Scan custom port ranges.
  • Output results to a file.

Resources

Learning Path

  • Learn C and systems programming basics.
  • Study networking fundamentals.
  • Explore advanced topics like multithreading and asynchronous I/O.

Happy Scanning!


This content originally appeared on DEV Community and was authored by Jonathan Peters


Print Share Comment Cite Upload Translate Updates
APA

Jonathan Peters | Sciencx (2025-10-03T10:46:49+00:00) Building a simple TCP port scanner in C. Retrieved from https://www.scien.cx/2025/10/03/building-a-simple-tcp-port-scanner-in-c-2/

MLA
" » Building a simple TCP port scanner in C." Jonathan Peters | Sciencx - Friday October 3, 2025, https://www.scien.cx/2025/10/03/building-a-simple-tcp-port-scanner-in-c-2/
HARVARD
Jonathan Peters | Sciencx Friday October 3, 2025 » Building a simple TCP port scanner in C., viewed ,<https://www.scien.cx/2025/10/03/building-a-simple-tcp-port-scanner-in-c-2/>
VANCOUVER
Jonathan Peters | Sciencx - » Building a simple TCP port scanner in C. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/03/building-a-simple-tcp-port-scanner-in-c-2/
CHICAGO
" » Building a simple TCP port scanner in C." Jonathan Peters | Sciencx - Accessed . https://www.scien.cx/2025/10/03/building-a-simple-tcp-port-scanner-in-c-2/
IEEE
" » Building a simple TCP port scanner in C." Jonathan Peters | Sciencx [Online]. Available: https://www.scien.cx/2025/10/03/building-a-simple-tcp-port-scanner-in-c-2/. [Accessed: ]
rf:citation
» Building a simple TCP port scanner in C | Jonathan Peters | Sciencx | https://www.scien.cx/2025/10/03/building-a-simple-tcp-port-scanner-in-c-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.