+ Reply to Thread
Results 1 to 3 of 3

Thread: Simple TCP port scanner for beginners

  1. #1
    Newbie billy786 is on a distinguished road
    Join Date
    Jun 2008
    Posts
    9

    Talking Simple TCP port scanner for beginners

    This is a simple port scanner created by me when i was learning socket programming in C. I thought it might be usefull to someone and decide to post it hope you enjoy it.

    The code is using tcp connect with an if statement to determine whether the port is opened or closes within a for loop which is for the port range.
    Code:
    /* A TCP port scanner created by billy
    www.softhardware.co.uk*/
    
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdlib.h>
    #include <errno.h>
    
    
    /* Main programs starts*/
    int main(int argc, char **argv)
    {
       int   sd;         //socket descriptor
       int    port;         //port number
       int   start;         //start port
       int    end;         //end port
       int    rval;         //socket descriptor for connect   
       char    responce[1024];      //to receive data
       char   *message="shell";       //data to send
       struct hostent *hostaddr;   //To be used for IPaddress
       struct sockaddr_in servaddr;   //socket structure
       
       if (argc < 4 )
       {
          printf("------Created By www.h4ck-y0u.org-----------\n");
          printf("--------------------------------------------------\n");
          printf("Usage: ./tscan <IPaddress> <Start Port> <End Port>\n");
          printf("--------------------------------------------------\n");
          return (EINVAL);
       }
       start = atoi(argv[2]);
       end   = atoi(argv[3]);
       for (port=start; port<=end; port++)
       {
    
             //portno is ascii to int second argument      
    
       sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //created the tcp socket
       if (sd == -1)
       {
         perror("Socket()\n");
         return (errno);
       }   
    
       memset( &servaddr, 0, sizeof(servaddr));
    
       servaddr.sin_family = AF_INET;
       servaddr.sin_port = htons(port); //set the portno
       
       hostaddr = gethostbyname( argv[1] ); //get the ip 1st argument
       
       memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);
          
       //below connects to the specified ip in hostaddr
       
     
    
       rval = connect(sd, (struct sockaddr *) &servaddr, sizeof(servaddr));
       if (rval == -1)
       {
       printf("Port %d is closed\n", port);
       close(sd);
       }
       else
       printf("Port %d is open\n",port);
       
       close(sd);         //socket descriptor
       }
       
    }
    Last edited by WingedPanther; 06-20-2008 at 11:10 AM. Reason: add code tags

  2. #2
    Newbie torleif is an unknown quantity at this point
    Join Date
    Jun 2008
    Posts
    2

    Re: Simple TCP port scanner for beginners

    Nice and simple, also I see another fan of the unix like #include <sys/types.h>. I only learnt about this recently and was using a complicated cross platform library

  3. #3
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    Re: Simple TCP port scanner for beginners

    You should post this in the Tutorials section. Very nice. +rep given.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Project: ionFiles - Joomla Simple File Download
    By Jordan in forum Community Projects
    Replies: 369
    Last Post: 01-30-2010, 01:10 PM
  2. VB 6.0: Tutorial, Making a Port Scanner
    By TcM in forum VB Tutorials
    Replies: 119
    Last Post: 08-24-2009, 02:59 PM
  3. Why Port 80 ??
    By bodhi2016 in forum C and C++
    Replies: 2
    Last Post: 06-20-2008, 02:14 PM
  4. Probably a simple class question
    By utdiscant in forum Python
    Replies: 1
    Last Post: 06-10-2008, 09:10 PM
  5. Help with simple project..?
    By minakrn in forum Java Help
    Replies: 1
    Last Post: 09-12-2007, 04:32 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts